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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…