const char* 與char*const的區(qū)別
C++標(biāo)準(zhǔn)規(guī)定,const關(guān)鍵字放在類(lèi)型或變量名之前等價(jià)的。
const int n=5; //same as below
int const m=10
const char * pstr; // pstr 是字符指針,它指向的量是 const的,
例如: char *m= "hello world 1";
char *n= "Hello world 2";
const char * pstr=m;
pstr[1]= 'H '; // it 's wrong
pstr=n; // it 's right
char* const pstr;// pstr 是字符指針,這個(gè)指針的值必須初始化,初始化以后就不能改變了.
例如: char *m= "hello world 3";
char *n= "Hello world 4";
char* const pstr=m;
pstr=n; // it 's wrong
pstr[1]= 'H '; // it 's right
至于 const char* const pstr 自然是兩者的結(jié)合了.
const int n=5; //same as below
int const m=10
const char * pstr; // pstr 是字符指針,它指向的量是 const的,
例如: char *m= "hello world 1";
char *n= "Hello world 2";
const char * pstr=m;
pstr[1]= 'H '; // it 's wrong
pstr=n; // it 's right
char* const pstr;// pstr 是字符指針,這個(gè)指針的值必須初始化,初始化以后就不能改變了.
例如: char *m= "hello world 3";
char *n= "Hello world 4";
char* const pstr=m;
pstr=n; // it 's wrong
pstr[1]= 'H '; // it 's right
至于 const char* const pstr 自然是兩者的結(jié)合了.
posted on 2010-12-23 15:53 天下 閱讀(3961) 評(píng)論(0) 編輯 收藏 引用 所屬分類(lèi): C/C++