在路上...

The development of life
我们一直都在努力,有您的支持,将走得更远...

站内搜索: Google

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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 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

 
posted on 2009-09-02 23:04  palam  阅读(465)  评论(0编辑  收藏  举报