const char*, char const* and char *const 的區(qū)別
const char*, char const*, char*const的區(qū)別問題幾乎是C++面試中每次都會有的題目。
事實上這個概念誰都有只是三種聲明方式非常相似很容易記混。
Bjarne在他的The C++ Programming Language里面給出過一個助記的方法:
把一個聲明從右向左讀。 char * const cp; ( * 讀成 pointer to
cp is a const pointer to char
const char * p;
p is a pointer to const char;
char const * p;
同上因為C++里面沒有const*的運算符,所以const只能屬于前面的類型。
C++標準規(guī)定,const關鍵字放在類型或變量名之前等價的。
const int n=5;
//same as belowint const m=10;
const int *p; //same as below
const (int) * pint const *q; // (int) const *p
char ** p1;
//pointer to pointer to char
const char **p2;
//pointer to pointer to const char
char * const * p3;
//pointer to const pointer to char
const char * const * p4;
//pointer to const pointer to const char
char ** const p5;
//const pointer to pointer to char
const char ** const p6;
//const pointer to pointer to const char
char * const * const p7;
//const pointer to const pointer to char
const char * const * const p8;
//const pointer to const pointer to const char