Pointers & Memory · intermediate · ~10 min
Change a caller-owned pointer without changing its pointed-to integer.
A pointer is itself a variable. To let a function change a pointer owned by its caller, pass the address of that pointer. This adds one level of indirection; it does not require a linked list or an allocation.
If p is an int *, then &p has type int **. For int **pp = &p, *pp is the pointer stored in p, while **pp is the integer it points to.
Assigning through *pp changes the caller's pointer. Assigning through **pp changes the target integer. A pointer swap should exchange pointer values without modifying either target. The two outer pointers must be valid; their inner values can be NULL when you are only swapping those values.
A pointer is itself a variable. To let a function change a pointer owned by its caller, pass the address of that pointer. This adds one level of indirection; it does not require a linked list or an allocation.
#include <stdio.h>
void redirect(int **pp, int *target) { *pp = target; }
int main(void) {
int x = 1, y = 9;
int *p = &x;
redirect(&p, &y);
printf("%d %d\n", *p, x);
return 0;
}
Expected output on a successful allocation, where applicable:
9 1
The function receives the address of p and replaces the value stored there with the address of y. x is untouched. Dereferencing the updated p now reads y.
**a and **b: it changes the target values instead of the pointer variables.Use one dereference to change a caller's pointer and two to access its target. Pointer ownership and target ownership are separate concerns.