简体   繁体   English

指向数组的指针是否等于指向其第一个元素的指针?

[英]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*)&xfalse(void*)&x[9] == (void*)&xtrue

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.

相关问题 超过最后一个数组元素末尾的指针是否等于超过整个数组末尾的指针? - Shall a pointer past the end of the last array element compare equal to a pointer past the end of the whole array? 通过具有指向第一个元素的指针将数组分配给数组 - Assigning array to array by having pointer to its first element 是通过指向其第一个元素UB的指针访问多维数组的中间? - Is accessing the middle of a multidimensional array via a pointer to its first element UB? 指向数组第一个元素的指针的地址? - Address of a pointer to first element of an array? 指向数组的指针和指向数组第一个元素的指针之间的区别 - difference between pointer to an array and pointer to the first element of an array c ++,操作指向数组第一个元素的指针 - c++, manipulating the pointer to the first element of an array 指向成员的指针:指针和数组元素 - pointer to member: pointer and array element 指针值不同,但它们比较相等。 为什么? - Pointer values are different but they compare equal. Why? 为什么当我取消引用数组指针时,结果值是指向数组第一个元素的指针,而不是整个数组 object? - Why is it that, when I dereference an array pointer, the resulting value is a pointer to the first element of the array, not the entire array object? 将元素添加到指针数组 - Add element to pointer array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM