typedef struct {
//
} list;
vs
typedef struct list{
//
} list;
我在其他帖子中阅读到(例如使用匿名结构体与具有typedef的命名结构体),这些帖子说这两种方式几乎相同,只有在引用结构本身时才需要后者。
然而,以下代码在clang和gcc中都可以编译通过:
#include <stdio.h>
typedef struct {
struct list *next;
} list;
int main(){
list l;
return 0;
}
上面我有一个匿名结构体引用自身。这是如何编译通过的?
struct list
andlist
are different types . Struct tags have a different "namespace" to typedef names. You will encounter problems if you try to pointnext
to alist
- M.Mnext
作为指向名为struct list
的某个东西的指针,而这与当前正在定义的结构类型无关。请注意,使用typedef struct { struct asdfasdf *next; } list;
同样“有效”。 - Nate Eldredgestruct list*
就会成为与外部的list
相同类型的类型? - Dan