const 指针和普通指针可以混合吗?

发布于 2024-11-04 23:21:15 字数 209 浏览 0 评论 0原文

调用 printf() 时 *b 的值是否未定义?

void foo(int *a) {
  const int *b = a;
  int *c = a;
  *c = 2;
  printf("%d\n", *b); // what must be *b? 1, 2 or undefined?
}

int d = 1;
foo(&d);

Is the value of *b undefined when printf() is called?

void foo(int *a) {
  const int *b = a;
  int *c = a;
  *c = 2;
  printf("%d\n", *b); // what must be *b? 1, 2 or undefined?
}

int d = 1;
foo(&d);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

眉目亦如画i 2024-11-11 23:21:15

它将打印 2。 const int *b 字面意思是:

指向一个整数的指针,其值不能为
通过解除引用而改变。

这并不意味着指针指向的值可能不会改变。事实上,改变是完全正确的。使用此功能的一个可能场景是保留对某些大型结构的只读引用的结构。引用可能会改变,但使用该结构的函数可能不会改变指针后面的内容。

想象一个驱动程序或类似的驱动程序,它分发设备传递的任何数据的只读内存映射:映射的地址不是恒定的,但由于这是只读映射,用户程序可能无法写入它。 OTOH 当设备更新数据时,缓冲区的内容会改变,但映射地址不一定会改变。

It will print 2. const int *b literally means:

Pointer to an integer whose value cannot be
altered through its dereferentiation.

This does not mean that the value the pointer points to may not change. In fact it's perfectly valid to change. A likely scenario to use this, are structures that keep a read only reference to some large structure. The reference may change, but the functions working with that structure may not change what's behind the pointer.

Imagine a driver or similar that hands out a read only memory mapping of whatever data the device delivered: The address of the mapping is not constant, but since this is a read only mapping the user program may not write to it. OTOH when the device updates the data the contents of the buffer will change, but not necessarily the mapping address.

神也荒唐 2024-11-11 23:21:15

标准所说的是(重点是我的)

6.7.3/5

如果尝试修改定义的对象
通过使用带有非 const 限定类型的左值
const 限定类型,行为未定义。


这不适用于您的情况(恰恰相反)。
相关对象是用普通 (int) 类型定义的。

在您的情况下,只有通过 b 对对象进行的更改是非法的;通过 ac 进行的更改是完全合法的

What the Standard says is (emphasis is mine)

6.7.3/5

If an attempt is made to modify an object defined with
a const-qualified type
through use of an lvalue with non-
const-qualified type, the behavior is undefined.

This does not apply to your situation (just the other way around).
The object in question was defined with a plain (int) type.

In your situation only changes to the object through b are illegal; changes through a or c are perfectly legal

忆伤 2024-11-11 23:21:15

由于ba指向相同的内存,所以该值当然会改变。不知道为什么你还引入了 c ,但它没有添加任何东西。它将打印 2

Since b points at the same memory as a, the value will of course change. Not sure why you also introduced c though, but it doesn't add anything. It will print 2.

忆沫 2024-11-11 23:21:15

*b 将为 2,因为设置该值的 printf 之前的最后一行是 *c = 2

abc 都指向相同的整数值。所以最后一个设置它的将决定它的当前值。

*b would be 2 because the last line before the printf that sets the value is *c = 2.

a, b, and c all point to the same integer value. So the last one to set it will determine its current value.

留蓝 2024-11-11 23:21:15

printf() 中的 b 应为 2。您始终在 b 和 c 中处理指针。

b should be 2 in printf(). You handle a pointer all the time in b and c.

失退 2024-11-11 23:21:15

声明 const int *b = a; 表示 b 引用常量 int 值。也就是说它把它的值当作一个常数值。

因此,

*b = 10;

是不正确的,但是:

a = 10;

很好,因为 a 不是常量值,但是当取消引用 b 时,我们将其视为常量。

所以 b 肯定是定义的,因为您更改了它指向的值:

c = 2;

这与上面的示例相同。简而言之,指向 const 值的指针不能通过取消引用来修改该值,但可以通过其他方式更改它指向的值。

The declaration const int *b = a; means that b refers to a constant int value. Which is to say that it treats its value as a constant value.

Thus,

*b = 10;

is incorrect, but:

a = 10;

is fine since a is not a constant value, but when dereferencing b we treat it as constant.

So b is certainly defined since you changed the value it pointed to:

c = 2;

which is the same as the example above. Simply put, a pointer to a const value cannot modify the value through dereferencing, but the value which it points to can otherwise be changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文