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 pointerIndividual characters within the array may be changed but
amessage
will always refer to the same storage. Thepmessagepointer 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.
Note that in C you cannot assign arrays, so if you want to copy an array use
memcpyfunction or usestrcpyfunction to copy a string.