I stumbled upon this function in an answer to this question:
/* Note: I've formatted the code for readability. */
const char * getString() {
const char *x = "abcstring";
return x;
}
I was surprised to discover that returning a pointer to the literal string worked, and didn't segfault like I thought it would. I always assumed that literals were pushed on to the stack or put in some other temporary memory, but where limited to the scope of the function. But here it seems that they are more static than I had imagined. Are they then put into something sort of string pool that's global to the entire executable?
Also, is it the same thing if I pass a string literal as a parameter to a function? For example:
/* Where is the string literal in this example being placed? */
myfunc(value1, value2, "rainbowdash");
I hope someone can enlighten me. Thanks in advance! :)