常量指针与指针常量

本文深入解析C语言中指针与const修饰符的六种组合方式,通过易记的口诀帮助理解,并强调了const对象地址赋值的规则。适合C语言初学者参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值