有关C++指针的问题

来源:百度知道 编辑:UC知道 时间:2024/06/21 20:45:58
Question: A pointer to const can point to a variable, but a pointer to variable can not point to a const if the pointer is not a pointer to const, why?
为什么常量指针可以指向变量,变量指针不可以指向常量?

你可以反过来想一想:
如果变量指针可以指向常量,那么是不是可以通过这个变量指针来修改常量的值呢?这就失去了常量的特征了。
而常量指针指向变量表明无法通过这个常量指针来修改这个变量,这不会存在问题。

所以...

因为常量没法或得地址。

指针是指向某个地址的,常量无法取地址,所以不可以指向常量

一楼正解。

一楼正解。
2,3楼纯属胡说。 3楼居然匿名了,果然很邪恶~!
const char *p;
const char c='c';
p=&c;
一样能编译