const在C里面的各种用法及含义

1.有以下表达式:

int a=248; b=4;
int const c=21;
const int *d=&a;
 int *const e=&b;
int const *const f =&a;

以下解释每个表达式的意思

int main()
{
	int a = 248, b = 4;//a和b是一般的变量

	int const c = 21;//const修饰c,使得c成为常量不可修改

	const int* d = &a;//*d是常量,*d代表a的内容不可修改  指针d指向谁是可改的
	d = &b;//right
	//*d = &b;//error
	int* const e = &b;//指针e指向不可改变,*e代表的b的内容可修改是可以改变的
	*e = 12;//right
	//e = &a;//error


	int const* const f  = &a;//*f和f都不可以改变
	//*f = a;//error
	//f = &b;//error
}

总结:const需要注意它修饰的是谁,谁就是不可变的  他的右边是谁就代表谁不可修改

猜你喜欢

转载自blog.csdn.net/weixin_62456756/article/details/128292750