c++ - 通过下标 : legal by the C++ Standard or not? 获取一个过去的数组元素的地址

标签 c++ c standards language-lawyer

我已经看到它多次断言 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 that E1[E2] is identical to (*((E1)+(E2))). Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (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 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.

请注意,标准明确允许指针指向数组末尾之后的一个元素,前提是它们没有被取消引用。在 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/

相关文章:

c - 返回复合文字

c++ - 指针非类型模板参数的用途?

c - Makefile - 没有附加参数的行为

c - 如何将返回代码传递给 at_exit 函数

c++ - 可以将Gtk +应用程序编译为在Gtk 2和3上运行

javascript - 将属性名称隐式传递给对象方法的正确方法是什么?

c++ - 2 个 vector 与结构 vector 的效率

c++ - Linux 上的 Qt 应用程序如何崩溃并且堆栈中没有我的代码(只有 QT 代码和标准库)?

c++ - 识别有关可执行文件的信息

C++:POD 类型可以包含 const 非指针成员吗?