When a function requires a pointer as an argument, what it really needs is an address. To pass a pointer to a function, you can use a pointer variable or use the ampersand operator to pass the address of any variable. Within the function, a pointer, or address, passed is treated as a pointer: it's a memory location or, with the asterisk operator, it's the value at that location. By passing pointers to a function, your code can manipulate values directly without having to return them.
- When you pass a pointer to a function … you're passing its address, the memory location … stored in the pointer. So a function … that accepts a pointer is an argument. … Accepts a variable's address in memory. … In this exercise files, integer variable A … is initialized to two at line ten. … Its address is passed to the doubler function … in line 13. You see the ampersand … address of operator. And then, in the function, … no value is returned. But within the function, … the value at the address is doubled. Build and run. … And the value is modified without being returned. … That's because the doubler function … is able to modify the value indirectly … through its address. In this code, … pointer A is passed directly to the doubler … function at line 15. No ampersand … is needed 'cause it's a pointer. … Pointers must be initialized before they're used. … So variable A is assigned to the address … of variable B in line 13. Build and run. … And the value is doubled. … Passing pointers is one way you can return …
- Working with arrays
- Building a structure
- Creating an array of structures
- Testing characters
- Working with strings in C
- Using pointers to manipulate data
- Manipulating files
- Using command-line arguments
- Working with time functions
Skill Level Intermediate
1. Arrays and Structures
2. Characters and Strings
4. Files and the Operating System
5. The Interesting and Extraordinary
Next steps1m 52s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.