Consider the following code:
char* pointerTesting(void) {
char* test = "hello";
return test;
}
int main() {
char* string = pointerTesting();
printf("string: %s\n", string);
}
This has no problem compiling and running. However, in my understanding, this shouldn't work, as the memory allocated to the test
pointer is on the stack and it's destroyed when returning to main.
So the question is, how does this manages to work without a malloc in the pointerTesting() function?