const pointer 与 pointer to const variable 的区别

如果能分清  T const * prt ;

                 const T * prt;

                 T * const prt;

三者区别, 就可以不用往下看了

 

A const pointer essentially means you can’t change the pointer variable itself, but you can change the value it points to.

const pointer(常量指针) 表示这个指针是指向一个固定的地址,  不能一会指向这,一会指向那;

 

A pointer to const means you can change the pointer but not what it points to;

 

a pointer to const (指向常量的指针)表示指针指向的 变量是一个常量, 不能改变这个常量的针,  但却可以改变指针的值, 让它指向别的地方。

 

 

 

int a = 100;
int * const prt = &a;// 表示prt指针是 常量指针, 不能改它的值

//prt++; <- won't compile ,若想改变它, 编译的时候会报错

*prt = 250; //但是可以改变它指向的变量的值



 

int a = 100

int const * prt  = &a; //指向常量的指针,pointer to const variable

//也可以这么写

const int *prt = &a;  //指向常量的指针,pointer to const variable

//这两种写法区别不大, const int ,  int const 不区分前后, 与java中
//public static ,  static public 一样,不分前后

*prt = 200; //不能这么写,编译出错, 常量的值怎么可以改变呢

prt ++; //没关系, 指针的值可以随便改;

 

reference:

http://blog.voidnish.com/?p=37

http://www.allinterview.com/showanswers/36962.html

 

 

你可能感兴趣的:(html,Blog)