简体   繁体   中英

Shall a pointer to an array compare equal to a pointer to its first element?

Imagine that I have an array 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] . Ie (void*)x == (void*)&x is false and (void*)&x[9] == (void*)&x is true .

Would such implementation be conforming? The only thing I know it "violates" is a non-normative Note in [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:

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] .

In [conv.ptr]/2 it says:

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM