gpt4 book ai didi

c++ - 指向数组的指针是否应该等于指向其第一个元素的指针?

转载 作者:行者123 更新时间:2023-11-28 01:18:53 25 4
gpt4 key购买 nike

假设我有一个数组 int x[10]; 并且在我的 C++ 抽象机实现中,指向数组的指针不等于指向其“第一个”元素的指针(应用于 x 的数组到指针转换的结果)但比较等于指向“最后”元素 x[9] 的指针。 IE。 (void*)x == (void*)&xfalse(void*)&x[9] == (void*)&x > 为 true

这样的实现是否合规?我唯一知道它“违反”的是[basic.compound]/4中的非规范注释:

[Note: An array object and its first element are not pointer-interconvertible, even though they have the same address.  — end note]

最佳答案

参见 [basic.compound (6.9.2)]/3:

A value of a pointer type that is a pointer to [...] an object represents the address of the first byte in memory occupied by the object.

由于 x[0] 是数组的第一个元素(并且指定数组没有初始填充),所以 x 的第一个字节必须相同作为 x[0] 的第一个字节。

在 [conv.ptr]/2 中它说:

A prvalue of type “pointer to cv T ”, where T is an object type, can be converted to a prvalue of type “pointer to cv void”. The pointer value (6.9.2) is unchanged by this conversion

除了 (void *)&x 的值表示 x 的第一个字节的地址之外,我没有看到任何其他解释方式。

关于c++ - 指向数组的指针是否应该等于指向其第一个元素的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57618805/

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