What you had is this
void func(char *p)
{
int q = 13;
p = &q;
}
This means "make p point to q" and changes value of p
, which is just a variable inside the function. No variable value changes are reflected outside the function.
If you were to write this
void func(char *p)
{
int q = 13;
*p = q;
}
This would mean "make the variable to which p
points to change its value to 13" and that would be seen outside, meaning the variable var
in main would change its value (depends on endianness what it would be since it's int and not char as the pointer claims it to be).
If you want to change the pointer's value in main you need a double pointer:
void func(char **p)
{
int q = 13;
*p = &q;
printf("%d\n", *p);
}
This would mean "make the pointer to which p
points to point to a local variable q
" and in this case you would have a dangling pointer as you expected in main
.