多维数组 (C)Multidimensional Arrays (C)

下标表达式还可以有多个下标,如下所示:A subscript expression can also have multiple subscripts, as follows:


expression1  
[  
expression2  
] [  
expression3  
]...  

下标表达式从左至右关联。Subscript expressions associate from left to right. 首先计算最左侧的下标表达式 expression1[expression2]。The leftmost subscript expression, expression1[expression2], is evaluated first. 通过添加 expression1 和 expression2 得到的地址构成一个指针表达式;然后 expression3 将添加到此指针表达式,从而构成一个新的指针表达式,依此类推,直到添加最后一个下标表达式。The address that results from adding expression1 and expression2 forms a pointer expression; then expression3 is added to this pointer expression to form a new pointer expression, and so on until the last subscript expression has been added. 在计算最后一个下标表达式之后应用间接寻址运算符 (*),除非最终指针值寻址数组类型(请参阅以下示例)。The indirection operator (\*) is applied after the last subscripted expression is evaluated, unless the final pointer value addresses an array type (see examples below).

具有多个下标的表达式引用“多维数组”的元素。Expressions with multiple subscripts refer to elements of "multidimensional arrays." 多维数组是其元素为数组的数组。A multidimensional array is an array whose elements are arrays. 例如,三维数组的第一个元素是一个具有两个维度的数组。For example, the first element of a three-dimensional array is an array with two dimensions.

示例Examples

在下面的示例中,将使用 3 个元素声明名为 prop 的数组,其中每个元素都是 int 值的 4 x 6 数组。For the following examples, an array named prop is declared with three elements, each of which is a 4-by-6 array of int values.

int prop[3][4][6];  
int i, *ip, (*ipp)[6];  

prop 数组的引用如下所示:A reference to the prop array looks like this:

i = prop[0][0][1];  

上面的示例演示如何引用 int 的第二个单独的 prop 元素。The example above shows how to refer to the second individual int element of prop. 数组将按行存储,因此最后一个下标变化最快;表达式 prop[0][0][2] 引用数组的下一个(第三个)元素,依此类推。Arrays are stored by row, so the last subscript varies most quickly; the expression prop[0][0][2] refers to the next (third) element of the array, and so on.

i = prop[2][1][3];  

此语句是对 prop 的单个元素的更复杂的引用。This statement is a more complex reference to an individual element of prop. 此表达式的计算方式如下:The expression is evaluated as follows:

  1. 第一个下标 2 乘以 4 x 6 int 数组的大小,然后与指针值 prop 相加。The first subscript, 2, is multiplied by the size of a 4-by-6 int array and added to the pointer value prop. 结果将指向 prop 的第三个 4 x 6 数组。The result points to the third 4-by-6 array of prop.

  2. 第二个下标 (1) 乘以 6 元素 int 数组的大小,然后与 prop[2] 表示的地址相加。The second subscript, 1, is multiplied by the size of the 6-element int array and added to the address represented by prop[2].

  3. 6 元素数组的每个元素都是一个 int 值,因此最后一个下标 3 在与 int 相加之前将乘以 prop[2][1] 的大小。Each element of the 6-element array is an int value, so the final subscript, 3, is multiplied by the size of an int before it is added to prop[2][1]. 生成的指针将寻址到 6 元素数组的第四个元素。The resulting pointer addresses the fourth element of the 6-element array.

  4. 将对指针值应用间接寻址运算符。The indirection operator is applied to the pointer value. 结果是该地址处的 int 元素。The result is the int element at that address.

    下面两个示例演示未应用间接寻址运算符的情况。These next two examples show cases where the indirection operator is not applied.

ip = prop[2][1];  

ipp = prop[2];  

在上面的第一个语句中,表达式 prop[2][1] 是对三维数组 prop 的有效引用;它引用一个 6 元素数组(上面已声明)。In the first of these statements, the expression prop[2][1] is a valid reference to the three-dimensional array prop; it refers to a 6-element array (declared above). 由于指针值将寻址到一个数组,因此不会应用间接寻址运算符。Since the pointer value addresses an array, the indirection operator is not applied.

同样,第二个语句 prop[2] 中的表达式 ipp = prop[2]; 的结果是一个寻址到一个二维数组的指针值。Similarly, the result of the expression prop[2] in the second statement ipp = prop[2]; is a pointer value addressing a two-dimensional array.

另请参阅See Also

下标运算符:Subscript Operator: