Example 1 — Write through and repoint with a double pointer
The function changes which object cur points to.
#include <stdio.h>
static void point_to(int **pp, int *target) {
*pp = target;
}
int main(void) {
int x = 10;
int *p = &x;
int **pp = &p;
**pp = 20;
printf("x = %d\n", x);
int a = 1, b = 2;
int *cur = &a;
point_to(&cur, &b);
printf("*cur = %d\n", *cur);
return 0;
} How It Works
**ppfollows two arrows frompptox.point_toreceives the address ofcur, so*pp = targetchangescur.- Passing
curitself would only change a copy.
