[英]Shall a pointer to an array compare equal to a pointer to its first element?
Imagine that I have an array int x[10];
假设我有一个数组
int x[10];
and in my implementation of C++ abstract machine a pointer to the array does not compare equal to a pointer to its "first" element (the result of the array-to-pointer conversion applied to x
) but compares equal a pointer to the "last" element x[9]
. 在我的C ++抽象机实现中,指向数组的指针并不等于指向其“第一个”元素的指针(应用于
x
的数组到指针转换的结果),而是等于等于“最后一个”的指针“元素x[9]
。 Ie (void*)x == (void*)&x
is false
and (void*)&x[9] == (void*)&x
is true
. 即
(void*)x == (void*)&x
为false
和(void*)&x[9] == (void*)&x
为true
。
Would such implementation be conforming? 这样的实现会符合要求吗? The only thing I know it "violates" is a non-normative Note in [basic.compound]/4 :
我唯一知道它“违反”的是[basic.compound] / 4中的非规范性注释:
[Note: An array object and its first element are not pointer-interconvertible, even though they have the same address.
[注意:即使数组对象及其第一个元素具有相同的地址,也不能指针可互换。 — end note]
—尾注]
See [basic.compound (6.9.2)]/3: 参见[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.
指向对象的指针的指针类型的值表示该对象占用的内存中第一个字节的地址。
Since x[0]
is the first element of the array (and arrays are specified to have no initial padding), the first byte of x
must be the same as the first byte of x[0]
. 由于
x[0]
是数组的第一个元素(并且指定数组没有初始填充),因此x
的第一个字节必须与x[0]
的第一个字节相同。
In [conv.ptr]/2 it says: 在[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”.
可以将类型为“ pointer to cv T”的prvalue转换为类型为“ pointer to cv void”的prvalue,其中T是对象类型。 The pointer value (6.9.2) is unchanged by this conversion
通过此转换,指针值(6.9.2)保持不变
I don't see any other way to interpret that than that the value of (void *)&x
represents the address of the first byte of x
. 我没有看到任何其他方式来解释比的值
(void *)&x
代表的第一个字节的地址, x
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.