In the chapter 5 of K&R C (2nd edition), the following two statements are given and said to have a subtle difference (I am pasting the text as it is)
There is an important difference between these definitions:
char amessage[] = "now is the time"; /* an array */ char *pmessage = "now is the time"; /* a pointer */
amessage
is an array, just big enough to hold the sequence of characters and'\0'
that initializes it. Individual characters within the array may be changed butamessage
will always refer to the same storage. On the other hand,pmessage
is a pointer, initialized to point to a string constant; the pointer may subsequently be modified to point elsewhere, but the result is undefined if you try to modify the string contents.
I am not able to understand what they want to imply here. It says that for pmessage
, the result is undefined if you try to modify the string constants. What is the result they are talking about, how can we modify the string constants and why will the result be undefined ?
Also it says that amessage
always points to the same storage. Is this storage the storage the array is allocated when it gets defined?
Can someone please explain in a better way ? I would be very grateful !!