Possible Duplicate:
What is the difference between char s[] and char *s in C?
More of a general question rather than trying to fix something, I've been reading the C programming language book and they take care to make the distinction between
char amessage[] = "blah";
char *pmessage = "blah";
The difference being one is a char array and the other a pointer to a string constant. They say modifying the char array is acceptable but you shouldn't modify string constants as it triggers undefined behavior. My question is: isn't the string constant stored in memory the same way the char array is? Why can I modify it as in
char *p = "this is a string constant";
*(p+2) = 'a';
printf("%s", p);
Ends up printing "thas is a string constant" as you might expect. I can understand how it would make sense as a string constant shouldn't end up being changed at run time, as it might confuse others/yourself working on your code not expecting it's value to change but in purely functional terms what is wrong with it, what is the undefined behavior that might trigger and how mechanically could it backfire when a char array wouldn't? I'm just wondering if I'm missing something about how string constants work in memory and how they are seen by the compiler.