那么,这是两种情况下他们所指的错误吗?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. Thepmessage
pointer may subsequently be modified to point elsewhere, but the result is undefined if you try to modify the string contents...
对于数组,
amessage[] = "allocate to another address"; //wrong?
关于指针,
pmessage[0] = 'n'; //wrong?
我只想知道什么情况下违反这些规则。
谢谢。
pmessage[0]
肯定行不通。至于另一个问题:也许可以,但为什么不测试一下呢? - 11684char *p = amessage; p[0] = 'h';
是可以工作的。问题在于pmessage
指向一个字符串常量。 - ouahamessage[] = "allocate to another address";
存在一个问题:它在语法上不正确。 - Pascal Cuoq