c++ - 指针数组说明

标签 c++ c arrays pointers

*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/

相关文章:

c++ - 对大文件执行 FFT 的最快方法是什么?

c++ - 指向其值的双向链表的析构函数

c - 警告 : initialization from incompatible pointer type

c - 如何将非 const 动态多维数组传递给需要 const 多维数组的函数?

arrays - 如何在 UIPickerView 中获取用户选择并在其他地方使用它?

将字符数组复制到另一个而不复制换行符

c++ - 使用 Dart 端口将 Float32x4 数组从 native 主机发送到 Dart 虚拟机

c++ - 为什么虚拟表中有两个虚拟析构函数,非虚拟函数的地址在哪里(gcc4.6.3)

c - 为什么这个 C 代码输出 1

javascript - 语法错误: Unexpected identifier while Parsin XML Data Using Google App Script