标准中措辞怪异,涉及指针的比较 [英] Strange wording in the standard, concerning comparison of pointers

查看:103
本文介绍了标准中措辞怪异,涉及指针的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

§6.5.8\ 6 (关于>,<,< =,> =)

§6.5.8\6 (concerning >, <, <=, >=)

如果表达式P指向数组对象的元素,则表达式 表达式Q指向同一数组对象的最后一个元素,即 指针表达式Q + 1比较大于P.在其他所有情况下, 行为是不确定的.

If the expression P points to an element of an array object and the expression Q points to the last element of the same array object, the pointer expression Q+1 compares greater than P . In all other cases, the behavior is undefined.

上面的几个部分,第6.5.8节解释说,基本上,指针算术可以按预期在数组上工作.即int a[3]; int *p = a; int *q = &a[2]; //q-p == 3是有效的.但是,当我阅读以上内容时,q > p是UB.

Several sections above, §6.5.8, it is explained that basically, pointer arithmetic work as expected on arrays. That is int a[3]; int *p = a; int *q = &a[2]; //q-p == 3 is valid. However, as I read the above q > p is UB.

我想念什么?

推荐答案

首先,您引用了段落的一部分,第一部分解释了该段落所引用的内容,我在此处包括了该段落:

Firstly, you have quoted part of a paragraph, the first part explains what this is referencing, I include the paragraph here:

比较两个指针时,结果取决于指针中的相对位置 指向对象的地址空间.如果两个指向对象类型的指针都指向 相同的对象,或者都指向同一数组对象的最后一个元素,它们 比较相等.如果指向的对象是同一聚合对象的成员, 指向稍后声明的结构成员的指针比指向成员的指针大 在结构的较早位置声明,并指向具有较大下标的数组元素的指针 值比较下标值较低的指针指向同一数组元素的指针更大.指向同一个联合对象的成员的所有指针比较相等.如果 表达式P指向数组对象的元素,表达式Q指向同一数组对象的最后一个元素,指针表达式Q + 1比较大于P.在所有其他情况下,该行为是不确定的.

When two pointers are compared, the result depends on the relative locations in the address space of the objects pointed to. If two pointers to object types both point to the same object, or both point one past the last element of the same array object, they compare equal. If the objects pointed to are members of the same aggregate object, pointers to structure members declared later compare greater than pointers to members declared earlier in the structure, and pointers to array elements with larger subscript values compare greater than pointers to elements of the same array with lower subscript values. All pointers to members of the same union object compare equal. If the expression P points to an element of an array object and the expression Q points to the last element of the same array object, the pointer expression Q+1 compares greater than P. In all other cases, the behavior is undefined.

基本上,您所引用的位是指这样的事实,通常指针必须总是指向一个独立的对象,一个对象数组的元素或一个对象数组的结尾.如您所见,通常增加一个已经指向数组最后一个元素的指针将产生一个无效的指针,并且确实绝不能取消引用该指针,但是它可以用于一种特殊情况,即它可以设置或与其他指针进行比较.

Basically, the bit you were referencing refers to the fact that normally a pointer must always point either to a stand alone object, to an element of an array of objects or one past the end of an array of objects. As you can see, normally incrementing a pointer which already points to the last element of an array would yield an invalid pointer, and indeed this pointer in the standard must never be dereferenced, however it can be used for one special case which is that it can be set or compared to another pointer.

这在程序中很有用,在该程序中,您可以递增一个指针,然后检查它是否超出数组的末尾并终止.例如.

This is useful in a program in which you increment a pointer then check if it is past the end of the array and terminate if it does. For example.

int foo = 0;
int ArrSize = 6;
int bar[ArrSize];
while(foo < ArrSize)
{
    foo++;
    printf("%d", bar + 3 < bar + foo);
}

即使在foo指向数组末尾之外的最后一种情况下,也将是合法的.

Will be legal, even in the last case where foo points one beyond the end of the array.

请注意,此示例非常人为,但演示了这一点.

Note this example is very contrived but demonstrates the point.

如果不遵循此规则,该程序将是不确定的行为.

If not for this rule this program would be undefined behaviour.

这篇关于标准中措辞怪异,涉及指针的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆