Re: [問題] 字串指標問題
要怎麼了解 C 語言那些規定怎麼生出來的呢?個人建議可以從閱讀
官方的「理由說明書」(Rationale)開始:
http://www.open-std.org/Jtc1/sc22/wg14/www/C99RationaleV5.10.pdf
像這個字串字面常數(string literal)背後其實有一段更複雜的
故事:
| String literals are not required to be modifiable. This
| specification allows implementations to share copies of
| strings with identical text, to place string literals in
| read-only memory, and to perform certain optimizations.
| However, string literals do not have the type array of
| const char in order to avoid the problems of pointer
| type checking, particularly with library functions, since
| assigning a pointer to const char to a plain pointer to
| char is not valid. Those members of the C89 Committee
| who insisted that string literals should be modifiable
| were content to have this practice designated a common
| extension (see §J.5.5).
唉唉,正因為有人堅持型態是 char* 所以才那麼多新手問明明不是
const 為什麼不能改...
總之強烈推薦 Rationale 給對標準好奇的人。C++ 標準委員會大概
是太忙了(?),沒有寫出等價的文件出來。另外我也推《The New C
Standard》,裡面有收集一些有趣的資料可以參考。時間太多的人也可以
去官方網站或是 comp.std.c 翻一些吵來吵去的過程。希望這些對大家有
幫助。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.30.39
※ 編輯: Favonia 來自: 140.112.30.39 (12/23 01:59)
※ 編輯: Favonia 來自: 140.112.30.39 (12/23 02:01)
推
12/23 15:00, , 1F
12/23 15:00, 1F
※ 編輯: Favonia 來自: 140.112.30.39 (12/24 22:51)
討論串 (同標題文章)