Passing pointers to functions
10 min lesson
- Passing a pointer to a function lets the function read or change the caller's variable, because it receives the address instead of a copy.
- This matters because C passes arguments by value, so without pointers a function cannot modify the caller's data.
- Think of passing a pointer as giving a friend the address of your house instead of a photo of it. With the address, they can actually visit and change things.
- A function parameter like int *p receives an address, and inside the function *p reads or writes the caller's value.
- To call such a function, pass the address with &: increment(&count);.
- Arrays decay to pointers when passed to functions, so the function can read and modify the array's elements.
- The computer copies the pointer value (the address) into the parameter; the address points to the original variable in the caller.
- The syntax is: void increment(int *p) { (*p)++; } and calling increment(&count);.
- A realistic use: a function that swaps two variables, fills an array, or returns multiple results through pointer parameters.
- A surprising detail: the parentheses in (*p)++ are required; without them *p++ increments the pointer, not the value.
- The example program defines a function that increments a counter through a pointer and a function that swaps two values through pointers.
- Pass a pointer when a function needs to change the caller's variable.
- Use & at the call site and * inside the function to reach the value.
- Remember that arrays are already passed as pointers, so no & is needed for them.
- Watch the parentheses: (*p)++ changes the value, *p++ moves the pointer.
- Pointers make functions more powerful. Nice progress!
#include <stdio.h>
// Increment the value pointed to by p
void increment(int *p) {
// (*p)++ adds 1 to the value at the address, not to p
(*p)++;
}
// Swap the values pointed to by a and b
void swap(int *a, int *b) {
// Save the value at a in a temporary variable
int temp = *a;
// Copy the value at b into a
*a = *b;
// Copy the saved value into b
*b = temp;
}
int main(void) {
// A counter we want to change through a function
int count = 5;
// Pass the address of count so increment can modify it
increment(&count);
// Print the updated value
printf("Count: %d\n", count);
// Two values to swap
int x = 1;
int y = 2;
// Pass their addresses so swap can exchange them
swap(&x, &y);
// Print the swapped values
printf("x=%d y=%d\n", x, y);
return 0;
}What happens: Count: 6 x=2 y=1
Breaking it down
Pointer parameter
void increment(int *p)The function expects an address of an int. Inside, p holds the caller's address, not a copy of the value.
Modifying through pointer
(*p)++Adds 1 to the int at that address. The parentheses make sure the value is incremented, not the pointer.
Calling with address
increment(&count);Passes the address of count. The function can now modify count directly.
Swap function
void swap(int *a, int *b)Takes two addresses and exchanges the values at those addresses. This is impossible with plain int parameters.
Pass by value vs pass by pointer
| Style | Parameter | Can modify caller? | Call syntax |
|---|---|---|---|
| By value | int x | No, gets a copy | f(count) |
| By pointer | int *p | Yes, via *p | f(&count) |
| Array | int arr[] | Yes, decays to pointer | f(arr) |
A helpful analogy
Passing a pointer to a function is like giving a friend the address of your house instead of a photo. With a photo they can only look; with the address they can actually come over and move the furniture. The address is small to pass, and it reaches the real place.
Nesting rules
- 1.Use & at the call site for non-array variables and * inside the function to reach the value.
- 2.Arrays decay to pointers, so do not use & when passing an array name.
- 3.Check pointer parameters for NULL before dereferencing when a caller might pass nothing.
- 4.Write (*p)++ to change the value, and *p++ to move the pointer; the parentheses matter.
Common mistakes
Forgetting & at the call site
Avoid
increment(count); // passes valueUse instead
increment(&count);Without &, the function receives the value, not the address. The compiler usually warns about incompatible pointer types.
Incrementing the pointer instead of the value
Avoid
*p++; // moves the pointerUse instead
(*p)++; // changes the value++ binds tighter than *, so *p++ increments p and then dereferences. Use parentheses to change the value.
Passing an array with &
Avoid
fill(&arr); // wrong typeUse instead
fill(arr);An array name already decays to a pointer to its first element. Adding & gives a pointer to the whole array, which is a different type.
Quick tips
- •Read int *p in a parameter as 'the function needs the address of an int'.
- •Document in a comment whether a pointer parameter may be NULL.
- •Try swapping without pointers to see it fail; it makes the lesson stick.
- •Use const int *p when the function only reads the value.
- •Practice passing an array to a function that fills it with values.