constchar *p 和 char const *p以及char *const p的含义

 const char* p <==> char const *p

const char*, char const*, char*const的区别问题几乎是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++标准规定,const关键字放在类型或变量名之前等价的。
const int n=5; //same as below
int const m=10;

const int *p; //same as below const (int) * p
int 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

你可能感兴趣的:(c,面试)