From the course: C: Data Structures, Pointers, and File Systems
Unlock the full course today
Join today to access over 22,400 courses taught by industry experts or purchase this course individually.
Passing pointers to functions - C Tutorial
From the course: C: Data Structures, Pointers, and File Systems
Passing pointers to functions
- 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 multiple values…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
Understanding pointers3m 19s
-
Using pointers to manipulate data2m 28s
-
Doing pointer math2m 57s
-
Comparing arrays and pointers3m 55s
-
Allocating storage3m 15s
-
Working with a pointer array3m 38s
-
Using pointers in structures3m 30s
-
Passing pointers to functions2m 57s
-
Returning pointers2m 39s
-
Challenge: Create a pointer array54s
-
Solution: Create a pointer array2m 23s
-
-
-
-