Suppose the following simple code:
int main(void){
char *p;
int i = 1;
while(i){
char str[] = "string";
p = str;
i = 0;
}
/* Can I use above string by using `p` in here? */
return 0;
}
I declared a string (char array) as local variable only valid in while{}
. But I saved its array address to the pointer p
which is also valid outside while{}
. Is it okay use the string outside while{}
by using p
?
If it is okay, why does it work?