【发布时间】:2021-08-02 06:46:52
【问题描述】:
typedef struct {
//
} list;
对
typedef struct list{
//
} list;
我在另一篇文章(例如Using an anonymous struct vs a named struct with typedef)中读到,其中说这两者几乎是等价的,唯一需要后者的时候是在引用结构本身时。
但是,使用 clang 和 gcc 可以很好地编译以下内容:
#include <stdio.h>
typedef struct {
struct list *next;
} list;
int main(){
list l;
return 0;
}
上面我有一个匿名结构引用它自己。这是如何编译的?
【问题讨论】:
-
struct list和list是不同的类型。结构标签与 typedef 名称有不同的“命名空间”。如果您尝试将next指向list,则会遇到问题 -
您将
next声明为指向struct list的指针,该指针与当前定义的结构类型无关。请注意,它与typedef struct { struct asdfasdf *next; } list;一样“好” -
我明白了,所以如果我使用后者,那么
struct list*将与外部list的类型相同?
标签: c struct declaration typedef forward-declaration