char *和const char *是相同的类型吗?

两者的数据类型还是不一样的:

	char* a = "hello, world!";
	const char* b = "hello, world!";

	cout << typeid(a).name() << endl;	// 输出:Pc
	cout << typeid(b).name() << endl;	// 输出:PKc

虽然char* = “hello” 和const char* ="hello"有差不多的用法,但是仍然是不一样的。比如说char*可以通过其他方式修改,而const char *就不能修改。

	char aa[] = {"hello!"};
	char* a = aa;
	*(a + 1) = 'E';
	cout << a << endl;  

这说明,两个语义很相似,都是指向一个常量,不能直接修改,但是前者可以通过其他方法修改。

猜你喜欢

转载自blog.csdn.net/m0_53061304/article/details/122891751