N
niclane
Hi,
I was reading section 5.5 of Ritchie and Kernighan and saw the
following:
"
.....
char amessage[] = "now is the time";
char *pmessage = "now is the time";
.....
pmessage is a pointer, initalized 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.
"
Why would the result be undefined? Doesn't the initization create an
array of chars in memory terminated with a NULL and this is pointed to
by pmessage? In this case why could one of these elements of the array
be altered? The book says that the declaration of amessage would allow
individual chars to be altered. Which only makes me more confused since
aren't these two statements in the respect of the rhs the same?
Thanks,
Nic
I was reading section 5.5 of Ritchie and Kernighan and saw the
following:
"
.....
char amessage[] = "now is the time";
char *pmessage = "now is the time";
.....
pmessage is a pointer, initalized 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.
"
Why would the result be undefined? Doesn't the initization create an
array of chars in memory terminated with a NULL and this is pointed to
by pmessage? In this case why could one of these elements of the array
be altered? The book says that the declaration of amessage would allow
individual chars to be altered. Which only makes me more confused since
aren't these two statements in the respect of the rhs the same?
Thanks,
Nic