Warning : returns address of a local variable YET correct output
This problem was suggested to me by a friend.
The code is -
Code:
/* Returning a pointer to an out of scope variable */
# include <stdio.h>
int* fun(){
int k = 35;
return &k;
}
int main(void){
int *j;
j = fun();
printf("%d",*j);
return 0;
}
Compiling this on GCC gives the above warning because k is a local variable and it will go out of scope once the function returns. Yet, when I run the program I get a perfectly correct output. That is 35. Why is this? Shouldn't I be getting garbage value?