gpt4 book ai didi

c++ - cpp中的指针

转载 作者:行者123 更新时间:2023-12-01 15:07:49 26 4
gpt4 key购买 nike

我在书中有下一个例子

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如何有效?

最佳答案

if pp2 is pointer-to-const?

pp2,而不是指向const的指针。它是指向const int的非常量指针。
这样,修改 *pp2(指向const int的非常量指针)没有问题。不允许的是修改const int的 **pp2

关于c++ - cpp中的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62969397/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com