Code:
void main()
{
int * j = fun();
printf("%d", *j);
}
int * fun()
{
int k = 35;
return &k;
}
The value 35 is printed in main. However, I expected a garbage value to be printed since k would be popped off the stack once the function fun() returns.
If I precede the printf statement by some other function call, *j prints out the wrong value implying that the stack contents changed & therefore the wrong value. Does that imply that k remained on stack all this while ?
Can anyone explain exactly how this is working ?