#include<stdio.h>
int* check(int,int);
int main()
{
int *c,t = 2;
c = check(10,20);
printf("t = %d\n",t);
printf("c = %d\n",*c);
return 0;
}
int* check(int i,int j)
{
int *p,*q;
p = &i;
q = &j;
if(i>=45)
return (p);
else
return (q);
}
I’m getting the output of code as :
t = 2
c = 2
why? because according to the concept the value returned to c is j address i.e. value of *c is 20 but as there is a printf statement preceding to the c’s printf statement, therefore, the value of the *c should have been some garbage value as stack is changed.
can anyone help me at this ? Please Help !!
It is the case of Dangling Pointer. Referencing Dangling pointers is
Undefinedwhen may give anyunpredictable value.