我總是在const指針中混淆。 有誰能夠解釋我簡單的方法是什麼試圖say.I以下 的代碼片段知道他們在說什麼,但我需要的原因 簡單way.Thanksconst限定符和Const指針
char *p="Hello"; /*pointer is variable,so string is*/
*p='M';
p="BYE"; /*works*/
p="Bye"; /*works*/
const char *q="Hello"; /*string is constant pointer is not */
*q='M'; /*Error*/
q="BYE"; /*works*/
char const *s="Hello"; /*string is constant pointer is not */
*s='M'; /*Error*/
s="BYE"; /*works*/
char* const t="Hello"; /*pointer is constant string is not */
*t='M'; /*works*/
t="BYE"; /*error*/
const char* const u="Hello"; /*string is constant,so is pointer*/
*u='M'; /*error*/
u="BYE"; /*error*/
不想聽起來令人滿意,圖表[在這個答案](http://stackoverflow.com/a/14566215/1322972)是一個不錯的詮釋,並將其應用於單向和雙向間接指針在不同的位置。並且總是有[** cdecl.org **](http://cdecl.org),如果您有關於type-decl的頻繁(或不頻繁)的問題,就值得加書籤。 – WhozCraig 2014-10-02 10:21:05
您似乎認爲字符串文字的解釋不同,具體取決於其地址分配給哪種類型的指針。不是這種情況。一個字符串文字* always *變成一個'char *'類型值,但是*你仍然不能改變字符*。 – unwind 2014-10-02 10:23:38