int b = 10; int c = 15; const int *p1 = &b; //*p1 = 12; /* 错误 */ p1 = &c; /* 正确 */ cout << *p1 << endl; int * const p2 = &b; *p2 = c; /* 正确 */ //p2 = &c; /* 错误 */ cout << *p2 << endl;
注:const右侧靠近指针还是常量,是指针就是常量指针,是常量就是指针常量。
int b = 10; int c = 15; const int *p1 = &b; //*p1 = 12; /* 错误 */ p1 = &c; /* 正确 */ cout << *p1 << endl; int * const p2 = &b; *p2 = c; /* 正确 */ //p2 = &c; /* 错误 */ cout << *p2 << endl;
注:const右侧靠近指针还是常量,是指针就是常量指针,是常量就是指针常量。