本篇文章主要介绍了"const 修饰指针的问题",主要涉及到const方面的内容,对于C/C++jrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下:
const 修饰指针的问题判断法则沿着*号划一条线:如果const位于*的左侧,则const就是用来修饰指针所指向的变量,即指针指向为常量如果const位于*的...
const 修饰指针的问题
判断法则
沿着*
号划一条线:
- 如果const位于
*
的左侧,则const就是用来修饰指针所指向的变量,即指针指向为常量 - 如果const位于
*
的右侧,const就是修饰指针本身,即指针本身是常量
验证
#include #include int main()
{
int a = 1;
int b = 2;
/*const int XX 与 int const XX 等价*/constint* p1 = &a;
printf("%d",*p1);
//*p1 += 1; ERROR const此时修饰指向的内容,无法修改常量
p1 = &b;// OK const 此时不修改指针,故指针本身值可变intconst *p2 = &b;
//*p2 += 1; ERROR 无法修改常量
p2 = &a; //OK const 此时不修改指针,故指针本身值可变/*const 修饰指针本身*/int* const p3 = &a;
*p3 += 1; // OK p3本身是常量不可变,但是可以修改所执行的内容//p3 = &b; ERROR 无法修改p3本身,因为p3本身是常量constint* const p4 = &b;
//*p4 += 1; ERROR 指向的内容被修饰为常量//p4 = &a; ERROR 指针本身值也被修饰为常量//const (int*) p = &a; syntax error//(int*) const p = &b; syntax errorreturn EXIT_SUCCESS;
}
').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('
').text(i));
};
$numbering.fadeIn(1700);
});
});
以上就介绍了const 修饰指针的问题,包括了const方面的内容,希望对C/C++jrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播有兴趣的朋友有所帮助。
本文网址链接:http://www.codes51.com/article/detail_596454.html