firemail

标题: const char*, char const*, char*const的区别 [打印本页]

作者: hechengjin    时间: 2016-6-19 18:40
标题: const char*, char const*, char*const的区别

事实上这个概念谁都有,只是三种声明方式非常相似很容易记混。
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






欢迎光临 firemail (http://firemail.wang:8088/) Powered by Discuz! X3