Possible Duplicate:
Can a local variable's memory be accessed outside its scope?
I'm trying to understand why I get this output for the below program
[hello] [0xbfde68f4]
[world] [0xbfde68f4]
[world] [0xbfde68f4]
The program is
int main(void)
{
char **ptr1 = NULL;
char **ptr2 = NULL;
ptr1 = func1();
ptr2 = func2();
printf(" [%s] [%p]\n",*ptr1, (void*)ptr1);
printf(" [%s] [%p]\n",*ptr2, (void*)ptr2);
printf(" [%s] [%p]\n",*ptr1, (void*)ptr1);
return 0;
}
char** func1()
{
char *p = "hello";
return &p;
}
char** func2()
{
char *p = "world";
return &p;
}
I understand that it's not a good practice to return address of local variables but this is just an experiment.