<分区>
typedef struct test {
int a;
};
int main(void) {
test t;
t.a = 3;
}
以上不编译。但是,当我将结构更改为:
typedef struct {
int a;
}test;
一切正常。为什么是这样?我见过很多代码示例,其中 struct 与 typedef 在同一行,但它不是为我编译的。
标签 c
<分区>
typedef struct test {
int a;
};
int main(void) {
test t;
t.a = 3;
}
以上不编译。但是,当我将结构更改为:
typedef struct {
int a;
}test;
一切正常。为什么是这样?我见过很多代码示例,其中 struct 与 typedef 在同一行,但它不是为我编译的。
最佳答案
typedef
的通用语法是
typedef type-declaration alias-name;
| |
| |
| |
typedef struct {int a; } test; //2nd one is correct
| |
| |
typedef struct test { int a;} ; //You missed the synonym/alias name here
编辑
请参阅下面的 Eric Postpischil 评论
你只会得到一个警告:空声明中无用的存储类说明符
引用:- this
关于一个非常简单的 C 结构中的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18770578/