结构声明Structure Declarations

“结构声明”用于为类型命名和指定一系列可具有不同类型的变量值(称为结构的“成员”或“字段”)。A "structure declaration" names a type and specifies a sequence of variable values (called "members" or "fields" of the structure) that can have different types. 可选标识符(称为“标记”)为结构类型命名并可用于结构类型的后续引用。An optional identifier, called a "tag," gives the name of the structure type and can be used in subsequent references to the structure type. 该结构类型的变量保留该类型定义的整个序列。A variable of that structure type holds the entire sequence defined by that type. C 中的结构类似于其他语言中称为“记录”的类型。Structures in C are similar to the types known as "records" in other languages.

语法Syntax

struct-or-union-specifier:struct-or-union-specifier:
struct-or-union identifier opt{ struct-declaration-list }struct-or-union identifier opt{ struct-declaration-list }

struct-or-union identifierstruct-or-union identifier

struct-or-union:struct-or-union:
structstruct

unionunion

struct-declaration-list:struct-declaration-list:
struct-declarationstruct-declaration

struct-declaration-list struct-declarationstruct-declaration-list struct-declaration

结构内容定义为The structure content is defined to be

struct-declaration:struct-declaration:
specifier-qualifier-list struct-declarator-list ;specifier-qualifier-list struct-declarator-list ;

specifier-qualifier-list:specifier-qualifier-list:
type-specifier specifier-qualifier-list opttype-specifier specifier-qualifier-list opt

type-qualifier specifier-qualifier-list opttype-qualifier specifier-qualifier-list opt

struct-declarator-list:struct-declarator-list:
struct-declaratorstruct-declarator

struct-declarator-list , struct-declaratorstruct-declarator-list , struct-declarator

struct-declarator:struct-declarator:
declarator

结构类型的声明不为结构预留空间。The declaration of a structure type does not set aside space for a structure. 它只是结构变量的最新声明的模板。It is only a template for later declarations of structure variables.

前面定义的 identifier(标记)可用于引用在其他位置定义的结构类型。A previously defined identifier (tag) can be used to refer to a structure type defined elsewhere. 在本例中,只要该定义可见,struct-declaration-list 就不能重复。In this case, struct-declaration-list cannot be repeated as long as the definition is visible. 在定义结构类型之前,指向结构类型的结构和 typedef 的指针的声明可使用结构标记。Declarations of pointers to structures and typedefs for structure types can use the structure tag before the structure type is defined. 但是,在实际使用字段大小之前,一定会先遇到结构定义。However, the structure definition must be encountered prior to any actual use of the size of the fields. 这是类型和类型标记的不完整定义。This is an incomplete definition of the type and the type tag. 为了完成此定义,类型定义之后必须出现在相同的范围中。For this definition to be completed, a type definition must appear later in the same scope.

struct-declaration-list 指定结构成员的类型和名称。The struct-declaration-list specifies the types and names of the structure members. struct-declaration-list 参数包含一个或多个变量或位域声明。A struct-declaration-list argument contains one or more variable or bit-field declarations.

每个在 struct-declaration-list 中声明的变量都被定义为结构类型的成员。Each variable declared in struct-declaration-list is defined as a member of the structure type. struct-declaration-list 中的变量声明与本节中讨论的其他变量声明的形式相同,只不过声明不能包含存储类说明符或初始值设定项。Variable declarations within struct-declaration-list have the same form as other variable declarations discussed in this section, except that the declarations cannot contain storage-class specifiers or initializers. 结构成员可具有除类型 void、不完整类型或函数类型之外的任何变量类型。The structure members can have any variable types except type void, an incomplete type, or a function type.

不能将成员声明为具有其出现的结构的类型。A member cannot be declared to have the type of the structure in which it appears. 但是,可将成员声明为指向结构类型(只要该结构类型具有标记,该成员就会出现在其中)的指针。However, a member can be declared as a pointer to the structure type in which it appears as long as the structure type has a tag. 这使您可以创建结构的链接列表。This allows you to create linked lists of structures.

结构的范围与其他标识符的一样。Structures follow the same scoping as other identifiers. 结构标识符必须不同于具有相同可见性的其他结构、联合和枚举标记。Structure identifiers must be distinct from other structure, union, and enumeration tags with the same visibility.

struct-declaration-list 中的每个 struct-declaration 在列表中都必须是唯一的。Each struct-declaration in a struct-declaration-list must be unique within the list. 但是,struct-declaration-list 中的标识符名称不必与普通变量名称或其他结构声明列表中的标识符名称不同。However, identifier names in a struct-declaration-list do not have to be distinct from ordinary variable names or from identifiers in other structure declaration lists.

嵌套结构也可以访问,就像它们是在文件范围级别声明的一样。Nested structures can also be accessed as though they were declared at the file-scope level. 例如,给定以下声明:For example, given this declaration:

struct a  
{  
    int x;  
    struct b  
    {  
      int y;  
    } var2;  
} var1;  

以下两个声明都是合法的:these declarations are both legal:

struct a var3;  
struct b var4;  

示例Examples

以下示例演示了结构声明:These examples illustrate structure declarations:

struct employee   /* Defines a structure variable named temp */  
{  
    char name[20];  
    int id;  
    long class;  
} temp;  

employee 结构有三个成员:nameidclassThe employee structure has three members: name, id, and class. name 成员是一个包含 20 个元素的数组,idclass 分别为具有 intlong 类型的简单成员。The name member is a 20-element array, and id and class are simple members with int and long type, respectively. 标识符 employee 是结构标识符。The identifier employee is the structure identifier.

struct employee student, faculty, staff;  

本示例定义了三个结构变量:studentfacultystaffThis example defines three structure variables: student, faculty, and staff. 每个结构都有相同的包含三个成员的列表。Each structure has the same list of three members. 成员被声明为具有在前面的示例中定义的结构类型 employeeThe members are declared to have the structure type employee, defined in the previous example.

struct           /* Defines an anonymous struct and a */  
{                /* structure variable named complex  */  
    float x, y;  
} complex;  

complex 结构具有两个 float 类型的成员:xyThe complex structure has two members with float type, x and y. 结构类型没有标记,因此是未命名或匿名的。The structure type has no tag and is therefore unnamed or anonymous.

struct sample   /* Defines a structure named x */  
{  
    char c;  
    float *pf;  
    struct sample *next;  
} x;  

结构的前两个成员为 char 变量和指向 float 值的指针。The first two members of the structure are a char variable and a pointer to a float value. 第三个成员 (next) 被声明为指向正在定义的结构类型 (sample) 的指针。The third member, next, is declared as a pointer to the structure type being defined (sample).

当不需要命名的标记时,匿名结构很有用。Anonymous structures can be useful when the tag named is not needed. 当一个声明定义了所有结构实例时,就是这种情况。This is the case when one declaration defines all structure instances. 例如:For example:

struct  
{  
    int x;  
    int y;  
} mystruct;  

嵌入结构通常是匿名的。Embedded structures are often anonymous.

struct somestruct  
{  
    struct    /* Anonymous structure */  
    {  
        int x, y;  
    } point;  
    int type;  
} w;  

Microsoft 专用Microsoft Specific

编译器允许未确定大小或零大小的数组作为结构的最后一个成员。The compiler allows an unsized or zero-sized array as the last member of a structure. 如果常量数组在不同的情况下大小不同,这可能很有用。This can be useful if the size of a constant array differs when used in various situations. 此类结构的声明类似于以下形式:The declaration of such a structure looks like this:

struct identifier{ set-of-declarations type array-name[ ];};struct identifier{ set-of-declarations type array-name[ ];};

未确定大小的数组仅在作为结构的最后一个成员时才出现。Unsized arrays can appear only as the last member of a structure. 只要所有封闭结构中都不再进一步声明成员,那么包含未确定大小的数组声明的结构就可以嵌入其他结构中。Structures containing unsized array declarations can be nested within other structures as long as no further members are declared in any enclosing structures. 不允许有此类结构的数组。Arrays of such structures are not allowed. 当应用于此类型的变量或应用于此类型本身时,sizeof 运算符假定数组的大小为 0。The sizeof operator, when applied to a variable of this type or to the type itself, assumes 0 for the size of the array.

如果结构声明是另一结构或联合的成员,则可以在没有声明符的情况下指定该结构声明。Structure declarations can also be specified without a declarator when they are members of another structure or union. 字段名将提升到该封闭结构中。The field names are promoted into the enclosing structure. 例如,无名称结构类似于以下形式:For example, a nameless structure looks like this:

struct s  
{  
    float y;  
    struct  
    {  
        int a, b, c;  
    };  
    char str[10];  
} *p_s;  
.  
.  
.  
p_s->b = 100;  /* A reference to a field in the s structure */  

有关结构引用的信息,请参阅结构和联合成员See Structure and Union Members for information about structure references.

结束 Microsoft 专用END Microsoft Specific

另请参阅See Also

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