#include<iostream> using namespace std; int main(void) { int wife = 24; int girl = 18; //第一种:渣男 int* zha_nan = &wife; *zha_nan = 25; zha_nan = &girl; *zha_nan = 19; printf("girl : %d wife: %d\n", girl, wife); //第二种:直男 const int* zhi_nan = &wife; //int const* zhi_nan = &wife; //*zhi_nan = 26;编译器报错,不可修改 printf("直男老婆的年龄:%d\n", *zhi_nan); zhi_nan = &girl; printf("直男女朋友的年龄:%d\n", *zhi_nan); //第三种:暖男 int* const nuan_nan = &wife; *nuan_nan = 26; printf("暖男老婆的年龄:%d\n", *nuan_nan); //nuan_nan = &girl; 不允许指向别的地址 //第四种:超级暖男 //不允许指向别的地址,同时也不可修改指向变量的值 const int* const super_nuan_nan = &wife; //*super_nuan_nan = 28; /* 总结:看const离类型(int)近,还是离指针变量名近, 离谁近,就修饰谁,谁就不能变 */ system("pause"); return 0; }
.总结:看const离类型(int)近,还是离指针变量名近,离谁近,就修饰谁,谁就不能变.