一维数组One-Dimensional Arrays

后跟用方括号 ([ ]) 括起的表达式的后缀表达式是数组对象元素的下标表示形式。A postfix expression followed by an expression in square brackets ([ ]) is a subscripted representation of an element of an array object. 下标表达式表示在表示为以下形式时位于超出 postfix-expression 的 expression 位置的地址的值A subscript expression represents the value at the address that is expression positions beyond postfix-expression when expressed as


postfix-expression  
[  
expression  
]  

通常,postfix-expression 表示的值是一个指针值(如数组标识符),而 expression 是一个整数值。Usually, the value represented by postfix-expression is a pointer value, such as an array identifier, and expression is an integral value. 但是,从语法上来说,只需要一个表达式是指针类型,另一个表达式是整型。However, all that is required syntactically is that one of the expressions be of pointer type and the other be of integral type. 因此整数值可以位于 postfix-expression 位置,指针值可以位于 expression 的方括号中或“下标”位置。Thus the integral value could be in the postfix-expression position and the pointer value could be in the brackets in the expression, or "subscript," position. 例如,以下代码是合法的:For example, this code is legal:

// one_dimensional_arrays.c  
int sum, *ptr, a[10];  
int main() {  
   ptr = a;  
   sum = 4[ptr];  
}  

下标表达式通常用于引用数组元素,但您可以将下标应用于任何指针。Subscript expressions are generally used to refer to array elements, but you can apply a subscript to any pointer. 无论值的顺序如何,expression 必须用方括号 ([ ]) 括起来。Whatever the order of values, expression must be enclosed in brackets ([ ]).

通过将整数值添加到指针值,然后将间接寻址运算符 (*) 应用于结果,可以计算下标表达式。The subscript expression is evaluated by adding the integral value to the pointer value, then applying the indirection operator (\*) to the result. (有关间接寻址运算符的讨论,请参阅间接寻址运算符和 Address-of 运算符。)实际上,对于一维数组,假定 a 是指针,b 是整数,则以下四个表达式等效:(See Indirection and Address-of Operators for a discussion of the indirection operator.) In effect, for a one-dimensional array, the following four expressions are equivalent, assuming that a is a pointer and b is an integer:

a[b]  
*(a + b)  
*(b + a)  
b[a]  

根据加法运算符的转换规则(在相加运算符中提供),可通过将整数值乘以指针寻址的类型的长度来将整数值转换为地址偏移量。According to the conversion rules for the addition operator (given in Additive Operators), the integral value is converted to an address offset by multiplying it by the length of the type addressed by the pointer.

例如,假定标识符 line 引用 int 值的数组。For example, suppose the identifier line refers to an array of int values. 以下过程用于计算下标表达式 line[ i ]The following procedure is used to evaluate the subscript expression line[ i ]:

  1. 将整数值 i 乘以定义为 int 项的长度的字节数。The integer value i is multiplied by the number of bytes defined as the length of an int item. i 的转换值表示 i int 位置。The converted value of i represents i int positions.

  2. 此转换值将添加到原始指针值 (line) 以生成从 line 偏移 i int 个位置的地址。This converted value is added to the original pointer value (line) to yield an address that is offset i int positions from line.

  3. 间接寻址运算符将应用于此新地址。The indirection operator is applied to the new address. 结果是位于该位置(直观地说,就是 line [ i ])的数组元素的值。The result is the value of the array element at that position (intuitively, line [ i ]).

    下标表达式 line[0] 表示行的第一个元素的值,因为 line 表示的地址的偏移量为 0。The subscript expression line[0] represents the value of the first element of line, since the offset from the address represented by line is 0. 同样,表达式(如 line[5])引用了从行偏移 5 个位置的元素,或数组的第 6 个元素。Similarly, an expression such as line[5] refers to the element offset five positions from line, or the sixth element of the array.

另请参阅See Also

下标运算符:Subscript Operator: