C中的结构混淆

所以我正在浏览这个C教程 ,我找到了这些代码行:

struct Monster { Object proto; int hit_points; }; typedef struct Monster Monster; 

而且我认为如果是这样的话会更有意义:

 typedef struct { Object proto; int hit_points; } Monster; 

我可能完全错了,因为我对C很新,但我会假设这两段代码都会做同样的事情。 他们这样做了,那么有什么理由更喜欢一个而不是另一个? 或者如果它们不同,是什么让它们与众不同? 谢谢!

有时候第二种forms不起作用。 假设您要创建Monster的链接列表。 使用第一个表单,您可以在struct添加指向下一个Monster的指针。

 struct Monster { Object proto; int hit_points; struct Monster* next; }; 

您不能在第二种forms中执行此操作,因为该struct没有名称。

第一段代码定义了一个类型struct Monster ,然后给它另一个名字Monster

第二段代码定义了没有标记的结构,并将其定义为Monster

使用任一代码,您都可以使用Monster作为类型。 但只有在第一个代码中,您还可以使用struct Monster

定义(从问题的第一部分 – 加上我的自由重组):

 struct Monster { Object proto; int hit_points; }; typedef struct Monster Monster; 

相当于:

 typedef struct Monster { Object proto; int hit_points; } Monster; 

我的偏好是:

 typedef struct MONSTER_S { Object proto; int hit_points; } MONSTER_T; 

仅供参考…不需要结构名称。 因此,如果代码只需要使用该类型,以下情况也可以:

 typedef struct { Object proto; int hit_points; } MONSTER_T;