1) The string is not (normally) on the stack -- it'll typically be in an initialized data segment that's read directly from the executable file. The pointer is then initialized to the address of that string.
2) No.
3) Because the standard says it gives undefined behavior. Consider if you had something like this:
int a() { char *a = "a"; printf("%s\n", a); }
int b() { char *b = "a"; *b = 'b'; }
int main() {
b();
a();
return 0;
}
Now, when you print out a
, do you expect to get the original value (a) or the updated valued (b)? Compilers can, but don't necessarily, share such static strings; some also mark that whole area read-only, so attempting to write to it will generate an exception.
From the viewpoint of the C standard, the only reasonable answer was to call it undefined behavior.