C++ const 与 指针
来源:互联网 发布:数控编程代码z代表什么 编辑:程序博客网 时间:2024/05/18 01:53
#includeusing namespace std;int main(){ int a1 = 13; int a2 = 19; int const p1 = 0; const int p2 = 0; // p1 和 p2 完全一样/* error: invalid conversion from 'const int *' to 'int *' '&p1' is 'const int *' 'p3' is 'int *' 此处声明的p3是int*的const,自身不可变,而指向的数据可变, 然而,现在指向的p1是const,不可变,所以编译器报错*/ // int * const p3 = &p1; int * const p3 = &a1; // ok// p3 = &a2; //p3 不可变, error: assignment of read-only variable 'p3' (*p3)++; // ok! equal a1++; int const * p4 = &p1; p4 = &a2; // ok!// (*p4)++; //p4 指向的数据不可变, error: increment of read-only location '* p4' const int * p5 = &p2; // p4 和 p5 完全一样 p5 = &a2;// (*p5)++; const int * const p6 = &a1; // ok!// p6 = &a2; // error: assignment of read-only variable 'p6'// (*p6)++; // error: increment of read-only location '*(const int*)p6' const int * const p7 = &p1; // ok!// p7 = &a2; // 同p6// (*p7)++; // 同p6 cout << "p3 = " << p3 << ", *p3 = " << *p3 << endl; cout << "p4 = " << p4 << ", *p4 = " << *p4 << endl; cout << "p5 = " << p5 << ", *p5 = " << *p5 << endl; cout << "p6 = " << p6 << ", *p6 = " << *p6 << endl; cout << "p7 = " << p7 << ", *p7 = " << *p7 << endl; return 0;}
运行结果:
- C++:const与指针
- C语言const与指针
- const与const指针
- C语言中的const与指针
- c的指针与复杂变量---const
- C/C++指针与const、数组、函数
- C经典 const与指针解析
- c++const指针与函数调用
- C指针与const的那些事
- C语言之const与指针
- 指针:const与指针
- C语言中const与指针的指针
- C/C++中的const--常量指针与指针常量
- c++const指针
- C中指针const
- C++:const和指针
- 【C和指针】const指针
- C语言中的const与指针的同址异值现象(指针究竟能不能修改const问题)
- 如何使用VC的预编译技术(转载)
- 位运算
- 写个小程序有效突破SAT词汇关
- mysql添加删除主键
- WPF中拖动控件,实现位置随意摆放
- C++ const 与 指针
- Python字符串统计以及排序
- 写给女友的JS教程---之JS闭包
- SQLite 数据库操作
- 网站
- jquery easyUI 中动态 改变 Datagrid中数据的方法
- 每日一题(42) - 二叉树中和为某一值的路径
- C++笔记II(奇数阶魔方的构造技巧)
- 在centos上设置计划任务