c++ 常量指针和指针常量
常量指针:
- const在*之前
- 指针的地址是可以被再次赋值的(可以修改的)
- 指针地址上面的值(变量)是不能被修改的
- 常量指针的常量是不能被改变的
指针常量:
- const在*之后
- 指针的地址是不可以被再次赋值的(不可以修改的)
- 指针地址上面的值(变量)是能被修改的
- 指针常量的指针地址是不能被改变的
int num = 5; int num2 = 10; int num3 = 2; // 常量指针,constant pointer, (keyword const before *) const int *i_ptr = # //can change the address of i_ptr pointed to i_ptr = &num2; //but cannot change it value //*i_ptr = 90; cout << "i_ptr value: " << *i_ptr << endl; // 指针常量,pointer constant, (keyword const after *) int *const i_prt2 = &num3; //cannot change the address of i_prt2 pointed to //&i_prt2 = &num2; //but can change it value *i_prt2 = 300; cout << "i_prt2 value: " << *i_prt2 << endl;
i_ptr value: 10 i_prt2 value: 300
标签:
c++
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)