常量指针与指针常量

int * const p;

const int * const p;

int const * const p;

这样的话,一共有六种,如下:

①const int p;
②const int* p;
③int const* p;

④int * const p;

⑤const int * const p;

⑥int const * const p;


第一种是常量整数,没什么好说的。

后面五种是指针,有一个简便的办法记忆。

从右往左读,遇到p就替换成“p is a ”遇到*就替换成“point to”。

比如说②,读作:p is a point to int const.

p是一个指向整型常量的指针。

③读作:p is a point to const int.

意思跟②相同。

④读作:p is a const point to int.

p是一个常量指针,指向整型。

⑤读作:p is a const point to int const.

⑥读作:p is a const point to const int.

⑤和⑥的意思相同,p都是常量指针,指向整型常量。


这种方法来源于《c primer plus》,这里也向c初学者推荐此书。




且注意:允许把非 const 对象的地址赋给指向 const 对象的指针,不允许把一个 const 对象的地址赋给一个普通的、非 const 对象的指针。

http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777416.html

原文地址:https://www.cnblogs.com/Pymcj/p/6723215.html