C/C++中char* p = "hello" 和 const char* p = "hello"的區別


在寫代碼常常都會寫char * p ="hello";這樣的代碼,雖然不是錯誤,但卻不建議這樣用。應該加const修飾。這句話背后的內涵是什么?下面就刨根問底一下:)

這個行為在不同的c和c++的編譯器上有會不同的行為,應該說char *a = "hello world";這種寫法不能算錯誤的寫法,所以c編譯器不報錯也不警告。早期的K&C認 為這是完全合法的,但從K&C第二版標准開始就明確指出“修改string literal的結果是未定義的,字符串並不總是可修改的,可能會放在只讀內存區域“。從C89開始,這個問題有了更明確的規定。char *a = "hello world";定義a是一種字符指針類並使用指定的字符數組(類型為array of char)“hello world"初始化,如果試圖修改數組的內容行為將是未定義的。關鍵的解釋就在"未定義“這個解釋上,實際上未定義不是錯誤,它在這里表示代碼不可移值。 所以在c89和c99規范的”公共擴展“部分有如下規則:String literals是可修改的,此時指向另外一個內容完全相同的對象。在c++中則有區別,c++2003標准規定string literal是由const字符數組構成的(array of n const char),所以C++明確了string literal是不可修改的。C++沒有規定所有內容相同的string literal會指向同一個對象(相同的內容指向唯一的對象),這是由編譯器實現決定的,同時指出了試圖修改string literal的行為是未定義的。為了兼容C,所以不加const 的char*依然是可用的,但通常編譯器會給出警告。最后C++標准明確規定不贊成這樣做(deprecated)。 修飾為deprecated表示將 來不再支持這個特性。


這個問題要解釋清楚就得看標准了。下面是相關標准中的信息摘要:

C99
char *p = "abc";
defines p with type ‘‘pointer to char’’ and initializes it to point to an object with type ‘‘array of char’’
with length 4 whose elements are initialized with a character string literal. If an attempt is made to use p to
modify the contents of the array, the behavior is undefined.

J.5.5 Writable string literals
1 String literals are modifiable (in which case, identical string literals should denote distinct
objects) (6.4.5).

C++2003
An ordinary string literal has type “array of n
const char” and static storage duration.

Whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementationdefined.
The effect of attempting to modify a string literal is undefined.

D.4 Implicit conversion from const strings [depr.string]
1 The implicit conversion from const to non-const qualification for string literals (4.2) is deprecated.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM