using namespace std;
int main(){
int a = 1;
int b = 5;
int c = 10;
int *const p1 = &a;
const int *p2 = &a;
cout<<&a<<endl;
cout<<&b<<endl;
cout<<&c<<endl;
cout<<p1<<endl;
cout<<*p1<<endl;
cout<<p2<<endl;
cout<<*p2<<endl;
*p1 = 2;//指针常量,指针不能修改,但是指针指向的值可以修改
//p1 = &b;//报错,显示左值不能修改,即这个指针不能修改
cout<<"---------------------"<<endl;
cout<<p1<<endl;
cout<<*p1<<endl;
cout<<a<<endl;
p2 = &c;//常量指针,指针可以修改,即这里原先指针指向的是a的地址,后来修改为c的地址
//*p2 = 9;//报错,指针指向的内容不能修改
cout<<"---------------------"<<endl;
cout<<p2<<endl;
cout<<*p2<<endl;
}
结果:
0x7ffeea3606bc
0x7ffeea3606b8
0x7ffeea3606b4
0x7ffeea3606bc
1
0x7ffeea3606bc
1
---------------------
0x7ffeea3606bc
2
2
---------------------
0x7ffeea3606b4
10