若纠结于const int* p,int const* p,int* const p这三个指针,可以看视频
本文只用const int* p,其他不使用,也不纠结了。
int* p 只能指向变量,可读可写。
const int* p 只读指针,可以指向变量、常量等,只读。
int x{ 10 }, y{11}; const int cx{ 20 }, cy{ 21 }; int* p;//只能指向变量,可读可改。 const int* cp;//只读指针。可以指向常量、变量,但是只读,不可更改。 p = &x;//指针p指向地址x p = &y;//指向地址y //p = &cx;//错误, *p = 30;//改变地址y中的值 cp = &x; cp = &y; cp = &cx; cp = &cy; //*cp = 30;//错误