I have a question about returning a local variable from a function by address. I understand that there is a lifetime for every local variable. But when I return an array and straight print for example the first and second cell I still get the values. But when I add only one line it is already destroyed. The question is why this does not happen right once the function closes.
int* f1();
int main(){
int *p=f1();
printf("%d %d\n",p[0],p[1]);
return 0;
}
int* f1(){
int arr[4]={1,2,3,4};
return arr;
}
output: 1 2
int* f1();
int main(){
int *p=f1();
printf("hey\n"); // **add this line**
printf("%d %d\n",p[0],p[1]);
return 0;
}
int* f1(){
int arr[4]={1,2,3,4};
return arr;
}
output: 1 11788742452