个人博客
C++中的常量指针和指针常量
常量指针
int a = 1;
int b = 2;
//定义一个常量指针
const int *p = &a;
cout << *p << endl;//输出1
p = &b;
cout << *p << endl;//输出2
//报错,常量指针不可以修改指针指向的值
//*p = 2;
指针常量
int a = 1;
int b = 2;
int * const p = &a;
cout << *p << endl;//输出1
*p = 2;
cout << *p << endl;//输出2
cout << a << endl;//输出2
//报错,指针常量不可以修改指针的指向
//p = &b;