ansi中的struct语法,带有逗号,只有一个类型声明用于多个字段

Yos*_*Yos 0 c

我在ansi C中遇到了这样的struct定义:

struct My_data {
    int a,
     b,
     c,
     d;
};
Run Code Online (Sandbox Code Playgroud)

和这种方式创建的结构完美无缺.我的问题是,这是否是ansi C中的特定法律语法.我在K&R中找不到任何此类信息.

usr*_*usr 5

是的,这是合法的.

它相当于:

struct My_data {
    int a, b, c, d;
};
Run Code Online (Sandbox Code Playgroud)

要么

struct My_data {
    int a;
    int b;
    int c;
    int d;
};
Run Code Online (Sandbox Code Playgroud)

甚至

struct My_data {
    int arr[4]; /* An array of 4 ints. This isn't strictly equivalent - but functionally equivalent */
};
Run Code Online (Sandbox Code Playgroud)

具体到您的问题,请参阅C11草案中的结构语法,6.7.2.1(以及C11,6.7.6 ,声明者的结构).