Consider the following code:
char* pointerTesting(void) {
char* test = "hello";
return test;
}
int main() {
char* string = pointerTesting();
printf("string: %s\n", string);
}
It has no problems compiling and running. However, in my opinion, this should not work, since the memory allocated to the pointer testis on the stack and is destroyed when returning to the main one.
So the question is, how does this work without malloc in the pointerTesting () function?
source
share