I want to modify my pointer's value in a function but I don't get why this example works.
#include<stdio.h>
void foo (int **p);
int main()
{
int i=97;
int *p = &i;
foo(&p);
printf("%d",*p);
return 0;
}
void foo (int **p)
{
int j=2;
*p = &j;
printf("%d",**p);
}
The output is 2 2 but how can it be possible if j doesn't exist anymore in the main? what does p point to? I would have expected that in the second time I printed the value of p(in the main) there will be garbage.