If I\'ve declared a pointer p
as int *p
; in main module, I can change the address contained by p
by assigning p=&a;
w
In C, variables are passed by value - a copy of the pointer is passed to the function. Use another pointer to the pointer instead:
void change(int **p, int *someOtherAddress)
{
*p = someOtherAddress;
}
int a = 1, b = 2;
int *p = &a;
printf("*p = %d\n", *p);
change(&p, &b);
printf("*p = %d\n", *p);
This prints
*p = 1
*p = 2