常量指针,修饰的类型,可以想象成*p是指向地址后面的值,那么const修饰值,那么值就不能改了,但是指针p指向的地址可以改
int* const p1;指针常量,修饰的p的地址,可以这样理解p是首地址,那么const修饰地址,那么地址就不能改了,但是指针p的值可以改
const int* const p2;双重const,所有的都不能修改
#includeusing namespace std; int main(int argc, char const* argv[]) { int temp = 9; int temp1 = 10; int* p1; p1 = &temp; //初始化 int* p3 = &temp; *p1 = 100; cout << *p1 << endl; //常量指针,修饰的类型,可以想象成*p是指向地址后面的值,那么const修饰值,那么值就不能改了,但是指针p指向的地址可以改 const int* p; p = &temp; //这个是ok的 p = &temp1; //这个是ok的 //*p = 100; //这个是error cout << *p << endl; //指针常量,修饰的p的地址,可以这样理解p是首地址,那么const修饰地址,那么地址就不能改了,但是指针p的值可以改 int* const p2= &temp; //p2 = &temp1; //这个是error的 *p2 = 100; //这个是ok cout << *p2 << endl; //双重const,所有的都不能修改 const int* const p4 = &temp; //p4 = &temp1; //这个是error的 //*p4 = 100; //这个是error的 cout << *p4 << endl; system("pause"); return 0; }



