结构声明

“结构声明”将类型命名为一个类型,并指定一系列变量值(称为“members”或“fields”),这些变量值可以具有不同的类型。 一个名为“标记”的可选标识符提供结构类型的名称,可用于对结构类型的后续引用。 该结构类型的变量包含该类型定义的整个序列。 C 中的结构类似于其他语言中称为“记录”的类型。

语法

struct-or-union-specifier:
struct-or-union identifier 选择{struct-declaration-list}
struct-or-union identifier

struct-or-union:
struct
union

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

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

specifier-qualifier-list:
type-specifier specifier-qualifier-list 选择
type-qualifier specifier-qualifier-list 选择

struct-declarator-list:
struct-declarator struct-declarator-list , struct-declarator

struct-declarator:
declarator
type-specifier declarator 选择:constant-expression

结构类型的声明不会为结构留出空间。 它只是结构变量的后续声明的模板。

以前定义的 identifier (标记)可用于引用在其他位置定义的结构类型。 在这种情况下,只要定义可见, struct-declaration-list 就不能重复。 为结构类型声明指向结构和 typedefs 的指针可以在定义结构类型之前使用结构标记。 但是,在实际使用字段大小之前,必须遇到结构定义。 此用法是类型和类型标记的不完整定义。 要完成此定义,必须稍后在同一作用域中显示类型定义。

指定 struct-declaration-list 结构成员的类型和名称。 参数 struct-declaration-list 包含一个或多个变量或位字段声明。

声明的每个 struct-declaration-list 变量都定义为结构类型的成员。 其中变量声明 struct-declaration-list 的形式与本节中讨论的其他变量声明相同,但声明不能包含存储类说明符或初始值设定项。 结构成员可以具有除类型 void、不完整类型或函数类型以外的任何变量类型。

无法将成员声明为具有其显示结构的类型。 但是,只要结构类型具有标记,就可以将成员声明为指向结构类型的指针。 它允许你创建结构的链接列表。

结构遵循与其他标识符相同的范围。 结构标识符必须与具有相同可见性的其他结构、联合和枚举标记不同。

列表中的每个 struct-declaration 属性都必须是唯一 struct-declaration-list 的。 但是,标识符 struct-declaration-list 名称不必与其他结构声明列表中的普通变量名称或标识符不同。

还可以访问嵌套结构,就像在文件范围级别声明它们一样。 例如,鉴于此声明:

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

这些声明都是合法的:

struct a var3;
struct b var4;

例子

这些示例说明了结构声明:

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

employee 结构有三个成员: nameid以及 class。 该 name 成员是一个 20 元素数组, id 并且 class 是具有 intlong 类型的简单成员。 标识符 employee 是结构标识符。

struct employee student, faculty, staff;

此示例定义三个结构变量: studentfacultystaff。 每个结构具有相同的三个成员的列表。 成员声明为具有在上一示例中定义的结构类型 employee

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

complex 结构具有两个类型 float 的成员, x 以及 y。 结构类型没有标记,因此未命名或匿名。

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

结构的前两个成员是变量 char 和指向值的 float 指针。 第三个成员 next声明为指向所定义的结构类型的指针(sample)。

当不需要标记名称时,匿名结构非常有用,例如,当一个声明定义所有结构实例时。 例如:

struct
{
    int x;
    int y;
} mystruct;

嵌入式结构通常是匿名的。

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

Microsoft 专用

编译器允许将未调整或零大小的数组用作结构的最后一个成员。 如果常量数组的大小在各种情况下不同,则它很有用。 此类结构的声明如下所示:

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

非化数组只能显示为结构的最后一个成员。 只要任何封闭结构中没有进一步的成员声明,包含未化的数组声明的结构就可以嵌套在其他结构中。 不允许此类结构的数组。 当应用于此类型的变量或类型本身时,运算符 sizeof 假定数组的大小为 0。

当结构声明是另一个结构或联合的成员时,还可以在没有声明符的情况下指定结构声明。 字段名称将提升为封闭结构。 例如,无名称结构如下所示:

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 */

有关结构引用的详细信息,请参阅 结构和联合成员

结束 Microsoft 专用

另请参阅

声明符和变量声明