数组声明Array Declarations

“数组声明”将命名数组并指定其元素的类型。An "array declaration" names the array and specifies the type of its elements. 它还可定义数组中的元素数。It can also define the number of elements in the array. 带数组类型的变量被视为指向数组元素的类型的指针。A variable with array type is considered a pointer to the type of the array elements.

语法Syntax

declarationdeclaration:
declaration-specifiers init-declarator-list opt;declaration-specifiers init-declarator-list opt;

init-declarator-listinit-declarator-list:
init-declaratorinit-declarator

init-declarator-list , init-declaratorinit-declarator-list , init-declarator

init-declaratorinit-declarator:
declaratordeclarator

declarator = initializerdeclarator = initializer

declaratordeclarator:
pointer optdirect-declaratorpointer optdirect-declarator

direct-declaratordirect-declarator:
direct-declarator [ constant-expression opt]direct-declarator [ constant-expression opt]

由于 constant-expression 是可选的,因此该语法有两种形式:Because constant-expression is optional, the syntax has two forms:

  • 第一种形式定义一个数组变量。The first form defines an array variable. 括号内的 constant-expression 参数指定数组中的元素数量。The constant-expression argument within the brackets specifies the number of elements in the array. constant-expression(如果有)必须具有整型类型和大于零的值。The constant-expression, if present, must have integral type, and a value larger than zero. 每个元素均具有 type-specifier 给定的类型,可以是除 void 之外的任何类型。Each element has the type given by type-specifier, which can be any type except void. 数组元素不能是函数类型。An array element cannot be a function type.

  • 第二种形式声明已在其他位置定义了变量。The second form declares a variable that has been defined elsewhere. 它省略括号中的 constant-expression 参数而不是括号。It omits the constant-expression argument in brackets, but not the brackets. 仅在之前已初始化数组、将其声明为参数或声明为对在程序中的其他位置显式定义的某个数组的引用的情况下才能使用此形式。You can use this form only if you previously have initialized the array, declared it as a parameter, or declared it as a reference to an array explicitly defined elsewhere in the program.

    在两种形式中,direct-declarator 都会命名变量并且可以修改变量的类型。In both forms, direct-declarator names the variable and can modify the variable's type. 紧跟 direct-declarator 的方括号 ([ ]) 会将声明符修改为数组类型。The brackets ([ ]) following direct-declarator modify the declarator to an array type.

    类型限定符可以出现在数组类型对象的声明中,但限定符应用于元素而不是数组本身。Type qualifiers can appear in the declaration of an object of array type, but the qualifiers apply to the elements rather than the array itself.

    您可以声明一系列数组(“多维”数组),方法是遵循以下形式的带括号的常量表达式的列表的数组声明符:You can declare an array of arrays (a "multidimensional" array) by following the array declarator with a list of bracketed constant expressions in this form:


type-specifier  
declarator [constant-expression] [constant-expression] ...  

方括号中的每个 constant-expression 均定义给定维度中的元素数量:二维数组具有两个带括号的表达式,三维数组具有三个带括号的表达式,依此类推。Each constant-expression in brackets defines the number of elements in a given dimension: two-dimensional arrays have two bracketed expressions, three-dimensional arrays have three, and so on. 如果您已初始化数组、将其声明为参数或声明为对在程序中的其他位置显式定义的某个数组的引用,则可以忽略第一个常量表达式。You can omit the first constant expression if you have initialized the array, declared it as a parameter, or declared it as a reference to an array explicitly defined elsewhere in the program.

可使用复杂的声明符定义指向各种类型的对象的指针的数组,如解释更复杂的声明符中所述。You can define arrays of pointers to various types of objects by using complex declarators, as described in Interpreting More Complex Declarators.

按行存储数组。Arrays are stored by row. 例如,下面的数组包含两个行,每个行具有三个列:For example, the following array consists of two rows with three columns each:

char A[2][3];  

首先存储第一行的三个列,然后存储第二行的三个列。The three columns of the first row are stored first, followed by the three columns of the second row. 这意味着最后一个下标的变化速度最快。This means that the last subscript varies most quickly.

若要引用数组的单个元素,请使用下标表达式,如后缀运算符中所述。To refer to an individual element of an array, use a subscript expression, as described in Postfix Operators.

示例Examples

这些示例阐释了数组声明:These examples illustrate array declarations:

float matrix[10][15];  

名为 matrix 的二维数组具有 150 个元素,其中每个元素都具有 float 类型。The two-dimensional array named matrix has 150 elements, each having float type.

struct {  
    float x, y;  
} complex[100];  

这是结构数组的声明。This is a declaration of an array of structures. 此数组有 100 个元素;每个元素均为一个包含两个成员的结构。This array has 100 elements; each element is a structure containing two members.

extern char *name[];  

此语句声明指向 char 的指针数组的类型和名称。This statement declares the type and name of an array of pointers to char. name 的实际定义会在其他位置出现。The actual definition of name occurs elsewhere.

Microsoft 专用Microsoft Specific

保存数组的最大大小所需的整数类型为 size_t 的大小。The type of integer required to hold the maximum size of an array is the size of size_t. 头文件 STDDEF.H 中定义的 size_t 是一个 unsigned int,其范围从 0x00000000 到 0x7CFFFFFF。Defined in the header file STDDEF.H, size_t is an unsigned int with the range 0x00000000 to 0x7CFFFFFF.

结束 Microsoft 专用END Microsoft Specific

请参阅See Also

声明符和变量声明Declarators and Variable Declarations