1 1 vote void main() { int *x; x =f(); printf("%d", *x); } int *f() { int y = 7 ; return &y; } Programming in C + – sumit goyal 1 1.2k views answer comment Share Follow Print See all 5 Comments 5 5 Comments reply Show 2 previous comments hs_yadav commented Jan 18, 2018 reply Follow flag it it he case of dagling pointer and here result of this execution would be compiler dependent ..... 0 0 replyShare sumit goyal 1 commented Jan 18, 2018 reply Follow flag @hs_yadav Bro according to me it should be 7 , i have done many questions on pointer like this only plz explain detial 0 0 replyShare MiNiPanda commented Jan 18, 2018 reply Follow flag You are trying to print the value of y. Here y is local to fun(). So after fun() call, fun() gets pushed onto the stack. Y gets some address there. Then after returning the address of y, the fun() pops out of the stack which means y also is no more present. Now the address that was allocated for y is lost. Which means x is pointing to some address which no more belongs to the given program. So when you try to access the value present in an address which is not part of your program, it gives error. 2 2 replyShare Please log in or register to add a comment.
0 0 votes yes, ans is unexpexted because when function definition is completed then local variable of function will discard abhicse answered Jan 18, 2018 abhicse comment Share Follow See 1 comment 1 1 comment reply sumit goyal 1 commented Jan 18, 2018 reply Follow flag Here you are not returning the value of y ..you are returning the address of y..so we can retain the value. 0 0 replyShare Please log in or register to add a comment.