int x=0;
int*a=&x;
void foo(int * a) {
static x=0;
x++;
printf("x's value %d ", x);
*a+=x;
a=&x;
*a=x+10;
}
int main(void) {
foo(a);
printf("a's value %d\n ", *a);
foo(a);
printf("a's value %d ", *a);
return 1;
}
I’m trying to analyze the above. First iteration of foo, when the function reaches to a=&x, the a after the function stops to get affected by what happens, since at the end of the function the pointer would go back to the original value he pointed to, now 1. the static x is now 1 as well. Second iteration: How’s x got the value 12?! the static x became 2, and so I expected 3 to be the value of a.
The output is:
x's value 1 a's value 1
x's value 12 a's value 13
The above code adds 10 to
x, because you setato be a pointer tox, and then set the value pointed to byatox+10.