我在书上有下一个例子
const int** pp2;
int* p1;
const int n = 13;
pp2 = &p1; // not allowed, but suppose it were
*pp2 = &n; // valid, both const, but sets p1 to point at n
*p1 = 10; // valid, but changes const n
但是,如果pp2是指向常量的指针,表达式*pp2=&n如何有效呢?
如果pp2是指向常量的指针?
pp2
不是指向常量的指针。 它是一个指向常量int的非常量指针。
因此,修改*pp2
是没有问题的,它是指向const int的非const指针。 不允许修改**pp2
,它是常量int。
pp2
不是“指向常量的指针”。 pp2是指向常量
的指针的指针。
因此,*pp2
是指向常量
的指针。 此指针本身不是常量
,将其修改为指向常量
的其他指针是完全有效的。