#include<stdio.h>
int* a(int* b){
int a = 20;
printf("\n \n");
if(a == 20){
printf("\n return from if a : 0x%x \n",a);
return &a;
}
else{
printf("\n returning from else b : 0x%x\n",b);
return b;
}
}
int main(){
int n = 10;
int *k,*m;
k = &n;
m = a(k);
printf("\n m ; 0x%x m : %d \n",m,*m);
return 0;
}
Here i'm returning the local variable of the function returning pointer. During function exit all the variables will bere moved from the stack memory , but how does the function still preserves the value at the address 'a' and prints the data ?
o/p :
return from if a : 0xbfd8cf14
m ; 0xbfd8cf14 m : 20
The address is retained in the pointer m and it prints the value correctly. I tried changing different no's.