本文翻译自:typedef struct vs struct definitions [duplicate]
This question already has an answer here: 这个问题已经在这里有了答案:
- Why should we typedef a struct so often in C? 为什么要在C语言中这么频繁地对结构进行typedef? 15 answers 15个答案
I'm a beginner in C programming, but I was wondering what's the difference between using
1 2 3 4 | struct myStruct{ int one; int two; }; |
vs. 与
1 2 3 4 | typedef struct{ int one; int two; }myStruct; |
#1楼
参考:https://stackoom.com/question/71pn/typedef-struct-vs-struct-definitions-重复
#2楼
The following code creates an anonymous struct with the alias
1 2 3 4 | typedef struct{ int one; int two; } myStruct; |
You can't refer it without the alias because you don't specify an identifier for the structure. 没有别名,就不能引用它,因为您没有为结构指定标识符。
#3楼
The
1 | struct myStruct blah; |
vs. 与
1 | myStruct blah; |
#4楼
In C (not C++), you have to declare struct variables like: 在C(不是C ++)中,您必须声明结构变量,例如:
1 | struct myStruct myVariable; |
In order to be able to use
1 2 | typedef struct myStruct someStruct; someStruct myVariable; |
You can combine
1 | typedef struct { ... } myStruct; |
#5楼
The difference comes in when you use the
The first way you have to do: 您必须做的第一种方式:
1 | struct myStruct aName; |
The second way allows you to remove the keyword
1 | myStruct aName; |
#6楼
If you use
1 | struct mystruct myvar; |
It's illegal to write 写是非法的
1 | mystruct myvar; |
If you use the