const

来源:互联网 发布:linux 代理客户端软件 编辑:程序博客网 时间:2024/06/05 16:56

reference : C++ Primer Plus

const*在const右边,意味着指向的数据不能修改。*在const左边,意味着指针是一个常量,地址不可以改变,但地址对应的数据可以改变。const int num = 5;int a[num] = {1,2,3,4,5};//c++中这样是可以的,但在c中不可以。void main(){ //num就是常量,就像是一个符号,不能被改变,但内存&num是可以改变的。 const int num = 10; int *p = (int *)# *p = 5; cout << num << endl;//输出10 cout << *p << endl;//输出5 cin.get();}常量不读内存,常量数组读内存#include "iostream"using namespace std;void change(const int *p){ int *pp = (int *)p; //去掉const属性 *pp = 111;}void main(){ int rat = 10; const int a = 20; const int b[5] = { 1, 2, 3, 4, 5 }; change(&rat); cout << rat << endl; //output 111 //常量不读内存 change(&a); cout << a << endl; //output 20 //常量数组会读内存 change(&b[4]); cout << b[4] << endl; //output 111 cin.get();}


0 0
原创粉丝点击