*variable[0] 和 variable[0][0] 是一回事吗? 第一个是指向数组第一个元素的指针。第二个是数组的第一个元素,它由指向数组的第一个元素指向。它们指向同一个元素吗?
最佳答案
根据C标准(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.3.2.1 左值、数组和函数指示符)
3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.
这个表达式
variable[0]
产生一个数组。对其应用一元运算符 *
数组将转换为指向其第一个元素的指针。所以
*variable[0]
等同于variable[0][0]
另一方面根据第一个引用表达式
variable[0][0]
等价于表达式 *( variable[0] + 0 )
而后者又等价于 *( variable [0] )
或只是 *variable[0]
关于c++ - 指针数组说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45226106/