forbiddens in string literals in C

c

Solution

/* OK, modifying an array initialized by the 
 * elements of a string literal */
amessage[0] = 'n';

/* not OK, modifying a string literal.
 * String literals are non-modifiable */
pmessage[0] = 'n';

Note that in C you cannot assign arrays, so if you want to copy an array use `memcpy` function or use `strcpy` function to copy a string.

Problem

In the K&R book page 104, I came across this statement: ``` char amessage[] = "now is the time"; //an array char *pmessage = "now is the time"; //a pointer ``` Individual characters within the array may be changed but `amessage` will always refer to the same storage. The `pmessage` pointer may subsequently be modified to point elsewhere, but the result is undefined if you try to modify the string contents... So, would this be the error they meant in both cases? For the array, ``` amessage[] = "allocate to another address"; //wrong? ``` For the pointer, ``` pmessage[0] = 'n'; //wrong? ``` I just want to know when one is going against these rules. Thanks.

Original source