黄骅贴吧桃花路,成都网站搜索引擎优化,wordpress播放代码,扬中人才const修饰变量是常用的#xff0c;不容易犯错#xff0c;而const和指针一起使用时很容易混淆。
(一)const int *p
#include stdio.hint main(void)
{int a 10;int b 20;const int *p a;*p b;return 0;
}
const在int *的左侧#xff0c;即指针指向内容为…const修饰变量是常用的不容易犯错而const和指针一起使用时很容易混淆。
(一)const int *p
#include stdio.hint main(void)
{int a 10;int b 20;const int *p a;*p b;return 0;
}
const在int *的左侧即指针指向内容为常量所以p指向的内容不允许修改编译器报错
修改成p b后编译通过因为这是修改指针p本身。 (二)int* const p
#include stdio.hint main(void)
{int a 10;int b 20;int* const p a;*p b;return 0;
}
const在int*的右侧即指针本身为常量所以*p b是允许的而*p b是不允许的。 (三)const int* const p
通过一二的例子举一反三可知两个const分别出现在int *的左右侧说明p不仅指针本身不能修改且p指向的内容也不能修改。