int * test1() {
int a = 4;
int * pa = &a;
return pa;
}
int * test2() {
int a = 4;
return &a;
}
What is the difference between the two functions?
The test1
function returns a value without a problem, but the test2
function generates a compile warning that the address value of the local variable can not be returned. Since the pa variable of the test1
function is also a local variable, does not the values of pa
and pa
disappear after the function ends? Is the variable a
of the test1
function and the variable pa
stored in a different memory area?