When you do something like:
char *f(){ return "static string"; }
You are returning the address of a string literal, but this string literal is not local to the function. Rather, it is statically allocated, so returning it gives clearly defined results (i.e., the String continues to exist after the function exits, so it works).
When you (try) to return the address of a char array as follows:
char *f() { char x[] = "automatically allocated space"; return x; }
The compiler allocates space for x on the stack, and then initializes it from a string literal that you do not have direct access to. What you are returning is a memory address on the stack, not a string literal, therefore, as soon as the function completes, this array ceases to exist, and you have no idea what else can be placed at that address. Attempting to use this address causes undefined behavior, which means that anything can happen.
source share