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

本节的其余部分描述了该列表中汇总的变量类型的声明的形式和含义。The rest of this section describes the form and meaning of declarations for variable types summarized in this list. 具体来说,其余各节说明如何声明以下内容:In particular, the remaining sections explain how to declare the following:

变量类型Type of Variable 说明Description
简单变量Simple variables 带整型或浮点型的单值变量Single-value variables with integral or floating-point type
阵列Arrays 由类型相同的一系列元素构成的变量Variables composed of a collection of elements with the same type
指针Pointers 指向其他变量并包含变量位置(以地址的形式)而不是值的变量Variables that point to other variables and contain variable locations (in the form of addresses) instead of values
枚举变量Enumeration variables 带整型的简单变量,其中包含命名整数常量组中的某个值Simple variables with integral type that hold one value from a set of named integer constants
结构Structures 由一系列可具有不同类型的值构成的变量Variables composed of a collection of values that can have different types
UnionsUnions 由占用相同的存储空间的不同类型的多个值构成的变量Variables composed of several values of different types that occupy the same storage space

声明符是声明的一部分,它指定要引入程序中的名称。A declarator is the part of a declaration that specifies the name that is to be introduced into the program. 它可以包括修饰符(如 \* (pointer-to))和任何 Microsoft 调用约定关键字。It can include modifiers such as \* (pointer-to) and any of the Microsoft calling-convention keywords.

Microsoft 专用Microsoft Specific

在声明符中In the declarator

__declspec(thread) char *var;  

char 是类型说明符,__declspec(thread)* 是修饰符,var 是标识符的名称。char is the type specifier, __declspec(thread) and * are the modifiers, and var is the identifier's name.

结束 Microsoft 专用END Microsoft Specific

可使用声明符声明值的数组、指向值的指针和返回指定类型的值的函数。You use declarators to declare arrays of values, pointers to values, and functions returning values of a specified type. 声明符出现在本节后面描述的数组和指针声明中。Declarators appear in the array and pointer declarations described later in this section.

语法Syntax

declarator:declarator:
  pointeropt direct-declarator  pointeropt direct-declarator

direct-declaratordirect-declarator:
  identifier  identifier
  ( declarator )  ( declarator )
  direct-declarator [ constant-expressionopt ]  direct-declarator [ constant-expressionopt ]
  direct-declarator ( parameter-type-list )  direct-declarator ( parameter-type-list )
  direct-declarator ( identifier-listopt )  direct-declarator ( identifier-listopt )

pointer:pointer:
  \* type-qualifier-listopt  \* type-qualifier-listopt
  \* type-qualifier-listopt pointer  \* type-qualifier-listopt pointer

type-qualifier-list:type-qualifier-list:
  type-qualifier  type-qualifier
  type-qualifier-list type-qualifier  type-qualifier-list type-qualifier

备注

有关引用 declarator 的语法,请参阅声明概述C 语言语法摘要中的 declaration 的语法。See the syntax for declaration in Overview of Declarations or C Language Syntax Summary for the syntax that references a declarator.

当声明符包含未修改的标识符时,正在声明的项将具有基类型。When a declarator consists of an unmodified identifier, the item being declared has a base type. 如果星号 (\) 显示在标识符的左侧,则将类型修改为指针类型。If an asterisk (**) appears to the left of an identifier, the type is modified to a pointer type. 如果标识符后跟方括号 ([ ]),则将类型修改为数组类型。If the identifier is followed by brackets ([ ]**), the type is modified to an array type. 如果标识符后跟圆括号,则将类型修改为函数类型。If the identifier is followed by parentheses, the type is modified to a function type. 有关解释声明中的优先级的详细信息,请参阅解释更复杂的声明符For more information about interpreting precedence within declarations, see Interpreting More Complex Declarators.

每个声明符至少声明一个标识符。Each declarator declares at least one identifier. 声明符必须包含一个类型说明符才能成为完整声明。A declarator must include a type specifier to be a complete declaration. 类型说明符提供了数组类型的元素的类型、由指针类型寻址的对象的类型或函数的返回类型。The type specifier gives the type of the elements of an array type, the type of object addressed by a pointer type, or the return type of a function.

数组指针声明将在本节后面做更详细地讨论。Array and pointer declarations are discussed in more detail later in this section. 以下示例阐释声明符的几种简单形式:The following examples illustrate a few simple forms of declarators:

int list[20]; // Declares an array of 20 int values named list  
char *cp;     // Declares a pointer to a char value  
double func( void ); // Declares a function named func, with no   
                     // arguments, that returns a double value  
int *aptr[10] // Declares an array of 10 pointers  

Microsoft 专用Microsoft Specific

Microsoft C 编译器不限制可修改算法、结构和联合类型的声明符的数目。The Microsoft C compiler does not limit the number of declarators that can modify an arithmetic, structure, or union type. 该数字仅受可用内存限制。The number is limited only by available memory.

结束 Microsoft 专用END Microsoft Specific

另请参阅See Also

声明和类型Declarations and Types