指针比较“>”中一个数组对象的第一个元素之前 [英] pointer comparisons “>” with one before the first element of an array object

查看:88
本文介绍了指针比较“>”中一个数组对象的第一个元素之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据该<一个href=\"http://stackoverflow.com/questions/16233868/pointer-comparisons-with-one-past-the-last-element-of-an-array-object\">topic,与比较指针的一个过去对象是允许数组的最后一个元素。

和根据 @jalf 注释,用比较指针的一个数组对象的第一个元素之前是被禁止的。

例1

  int数组[10];
INT * PTR;
为(PTR =安培;阵列[9]; PTR&GT;(数组1); ptr--){...}

示例2

  int数组[10];
INT * PTR;
对于(PTR =安培;阵列[9]; PTR&GT; =(数组); ptr--){...}

1)均为例1 示例2 禁止?

2)是否有与C标准证明是一个数组对象的第一个元素之前是被禁止的?

指针比较
解决方案

  1. 是的,都是不确定的行为:在第一种情况下,阵列-1 是不确定的;在第二种情况下, PTR 当迭代之后递减变得不确定时,它等于阵列

  2. 这是标准的第6.5.6所覆盖,部分8(重点是我的):


  

当具有整型一个前pression加到或从一个指针中减去,其结果具有指针操作数的类型。如果指针操作数指向数组对象的元素,并且阵列足够大,结果指向一个元件从原始元件偏移,使得所得到的和原始的数组元素的下标之差等于整数前pression。换句话说,如果EX pression P 指向数组对象,前pressions 第i个元素( P)+ N (等同于 N +(P))和(P)-N (其中 N 值为 N )指向,分别为 I + N -th和 I-N -th数组对象的元素,只要它们的存在。此外,如果前pression P 指向数组对象的最后一个元素,前pression (P)+1 点中的一个过去的数组对象的最后一个元素,如果前pression 问:点中的一个过去的数组对象的最后一个元素,前pression (Q)-1 指向数组对象的最后一个元素。如果指针操作数和结果指向相同的数组对象,或者一个过去的数组对象的最后一个元素的元素两者的评估也不得产生溢出; ,否则,这种行为是未定义。如果结果点中的一个过去的数组对象的最后一个元素,它不应被用作一元 * 运营商正在评估的操作数。


标准去长度覆盖的位置的一个过去的数组对象的最后一个元素的元素,而在位置的元素有一个事先的第一要素上述规则的,否则条款下下降。

According to this topic, comparing pointer with one past the last element of an array object is allowed.

And according to @jalf comment, comparing pointer with one before the first element of an array object is forbidden.

example1

int array[10];
int *ptr;
for(ptr=&array[9]; ptr>(array-1); ptr--) {...}

example2

int array[10];
int *ptr;
for(ptr=&array[9]; ptr>=(array); ptr--) {...}

1) Are both example1 and example2 forbidden?

2) Is there proof from the C standard that comparing pointer with one before the first element of an array object is forbidden?

解决方案

  1. Yes, both are undefined behavior: in the first case, array-1 is undefined; in the second case, ptr becomes undefined when it is decremented after the iteration when it is equal to array.
  2. This is covered by section 6.5.6 of the standard, part 8 (emphasis is mine):

When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integer expression. In other words, if the expression P points to the i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) and (P)-N (where N has the value n) point to, respectively, the i+n-th and i−n-th elements of the array object, provided they exist. Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object, and if the expression Q points one past the last element of an array object, the expression (Q)-1 points to the last element of the array object. If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined. If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated.

The standard goes length to cover the element at the position one past the last element of the array object, while the element at the position one prior the first element falls under the "otherwise" clause of the above rule.

这篇关于指针比较“&gt;”中一个数组对象的第一个元素之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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