Re: [問題] 字元指標的問題

作者: ando5566 (風雲變色)   2019-09-08 21:49:50
※ 引述《sakishil (亞凱)》之銘言:
: 這是常見問題
: 把字型雙引號改成單引號即可
: 例如"\t"改成'\t'
: 一個是指標一個是字型
: 來源 stack overflow
看你回得蠻有創意
我也分享我的看法
C++是later than C的語言
但compiler也是不斷地把新的且有意義的觀念納入
這郭問題簡單說就是
char* a = “hello”;時
正規來說 要使用const char*
因為hello 是你在記憶體裡暫時要一塊來放hello
並非允許你真的刻以後續去更改的
而char b[]={‘h’,’e’,’l’,’l’,’o’};時
要不要後續能更改就由你是否加const 來決定
另外在C++你試著實例化一個object
A a(“hello”);
Compiler會期待你的constructor寫為
A::A(const char* o)而非A::A(char * o)
這也就是compiler沒事會認為你在轉型const char * 與char*之間的原因。

Links booklink

Contact Us: admin [ a t ] ucptt.com