![](/img/trans.png)
[英]Shall a pointer past the end of the last array element compare equal to a pointer past the end of the whole array?
[英]Shall a pointer to an array compare equal to a pointer to its first element?
假设我有一个数组int x[10];
在我的C ++抽象机实现中,指向数组的指针并不等于指向其“第一个”元素的指针(应用于x
的数组到指针转换的结果),而是等于等于“最后一个”的指针“元素x[9]
。 即(void*)x == (void*)&x
为false
和(void*)&x[9] == (void*)&x
为true
。
这样的实现会符合要求吗? 我唯一知道它“违反”的是[basic.compound] / 4中的非规范性注释:
[注意:即使数组对象及其第一个元素具有相同的地址,也不能指针可互换。 —尾注]
参见[basic.compound(6.9.2)] / 3:
指向对象的指针的指针类型的值表示该对象占用的内存中第一个字节的地址。
由于x[0]
是数组的第一个元素(并且指定数组没有初始填充),因此x
的第一个字节必须与x[0]
的第一个字节相同。
在[conv.ptr] / 2中说:
可以将类型为“ pointer to cv T”的prvalue转换为类型为“ pointer to cv void”的prvalue,其中T是对象类型。 通过此转换,指针值(6.9.2)保持不变
我没有看到任何其他方式来解释比的值(void *)&x
代表的第一个字节的地址, x
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.