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.Code:void main() { int * j = fun(); printf("%d", *j); } int * fun() { int k = 35; return &k; }
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 ?



1Likes
LinkBack URL
About LinkBacks




but I will give it a try...but you do agree then that the output should be garbage ?