As far as my understanding and other referals i understood that when the address
of a local variable is returned the first call using this address may print the correct
value but if it is called after any other function may get undefined values.
but my program is returning the value that is equivalent to address.
please some one help me in understanding this.
int * first();
int * second();
int main()
{
int *f, *s ;
f = first();
printf(" After calling first : value of f ( %p ) \n", f);
printf(" After calling first : value of *f ( %x )\n", *f);
s = second();
printf(" After calling second : value of s ( %p ) \n", s);
printf(" After calling second : value of *s ( %x )\n", *s);
f = first();
printf(" After calling first second time : value of f ( %p ) \n", f);
printf(" After calling first second time : value of *f ( %x )\n", *f);
return 0;
}
int * first()
{
int f_lcl = 0xAAAA ;
printf(" In First : value of f_lcl ( %x ) \n", f_lcl);
printf(" In First : addr of f_lcl is ( %x ) \n", &f_lcl);
return &f_lcl ;
}
int * second()
{
int s_lcl = 0xFFFF;
printf(" In Second : value of s_lcl ( %x )\n", s_lcl);
printf(" In Second : addr of s_lcl is ( %x ) \n", &s_lcl);
return &s_lcl ;
}
the out i am getting is :
In First : value of f_lcl ( aaaa )
In First : addr of f_lcl is ( bff4f2c4 )
After calling first : value of f ( 0xbff4f2c4 )
After calling first : value of *f ( bff4f2c4 )
In Second : value of s_lcl ( ffff )
In Second : addr of s_lcl is ( bff4f2c4 )
After calling second : value of s ( 0xbff4f2c4 )
After calling second : value of *s ( bff4f2c4 )
In First : value of f_lcl ( aaaa )
In First : addr of f_lcl is ( bff4f2c4 )
After calling first second time : value of f ( 0xbff4f2c4 )
After calling first second time : value of *f ( bff4f2c4 )