其他分享
首页 > 其他分享> > 25.const修饰字符指针

25.const修饰字符指针

作者:互联网

const修饰字符指针

const修饰字符指针的三种情况

const char* p ; char const* p; char* const p;

const出现在*p前面(第1,2两种情况) 表示对p来说p被解引用的内容不可改,即p指向的内容无法被修改 const 出现在字符指针前表示字符指针的内容不可改

int main()
{
	char arr1[] = "abddef";
	const char* p1 = "abddef";
	char const *p2 = "abddef";

	char* const p3 = "abddef";

	arr1[2] = 'c';
	*(p1 + 2) = 'c';  //编译器报错error   *p不可改(p指向的内容不可被改)
	*(p2 + 2) = 'c';  //编译器报错error   *p不可改 (p指向的内容不可被改)

	p3 = &arr1[2];    //p3不可改(p本身不可改)
	*(p3 + 2) = 'c';  //*p3可改(p指向的内容可被改)

	return 0;
}

标签:25,abddef,const,p3,不可,char,修饰,指针
来源: https://www.cnblogs.com/best-you-articles-040612/p/16029384.html