我已经看到它多次断言 C++ 标准不允许以下代码:
int array[5];
int *array_begin = &array[0];
int *array_end = &array[5];
&array[5]
在这种情况下是合法的 C++ 代码吗?
如果可能的话,我想要一个引用标准的答案。
知道它是否符合 C 标准也很有趣。如果它不是标准 C++,为什么决定将其与 array + 5
或 &array[4] + 1
区别对待?
最佳答案
是的,这是合法的。来自 C99 draft standard :
§6.5.2.1,第 2 段:
A postfix expression followed by an expression in square brackets
[]
is a subscripted designation of an element of an array object. The definition of the subscript operator[]
is thatE1[E2]
is identical to(*((E1)+(E2)))
. Because of the conversion rules that apply to the binary+
operator, ifE1
is an array object (equivalently, a pointer to the initial element of an array object) andE2
is an integer,E1[E2]
designates theE2
-th element ofE1
(counting from zero).
§6.5.3.2,第 3 段(强调我的):
The unary
&
operator yields the address of its operand. If the operand has type ‘‘type’’, the result has type ‘‘pointer to type’’. If the operand is the result of a unary*
operator, neither that operator nor the&
operator is evaluated and the result is as if both were omitted, except that the constraints on the operators still apply and the result is not an lvalue. Similarly, if the operand is the result of a[]
operator, neither the & operator nor the unary*
that is implied by the[]
is evaluated and the result is as if the&
operator were removed and the[]
operator were changed to a+
operator. Otherwise, the result is a pointer to the object or function designated by its operand.
§6.5.6,第 8 段:
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 thei
-th element of an array object, the expressions(P)+N
(equivalently,N+(P)
) and(P)-N
(whereN
has the valuen
) point to, respectively, thei+n
-th andi−n
-th elements of the array object, provided they exist. Moreover, if the expressionP
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 expressionQ
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.
请注意,标准明确允许指针指向数组末尾之后的一个元素,前提是它们没有被取消引用。在 6.5.2.1 和 6.5.3.2 中,表达式 &array[5]
等价于 &*(array + 5)
,它等价于 (array+ 5)
,它指向数组末尾之后的位置。这不会导致取消引用(通过 6.5.3.2),因此它是合法的。
关于c++ - 通过下标 : legal by the C++ Standard or not? 获取一个过去的数组元素的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/988158/