我有一个全局双向链表结构,定义如下:
struct list {
struct list *next;
struct list *prev;
};
如果我尝试使用 sizeof(struct list)
初始化结构列表(在同一文件中) ,我得到invalid application of 'sizeof' to an incomplete type 'struct list'
。为什么会struct list
在这种情况下被认为是不完整的类型?该结构只有 2 个指针,其大小可以在编译时计算。
最佳答案
因为你没有向我们展示 Minimal, Complete, and Verifiable Example我只能猜测,但我猜测您没有包含定义结构的头文件,并且只有一个前向声明。
该前向声明仅告诉编译器该结构存在,但没有说明有关其成员或结构的大小。为此,您需要完整的定义。
关于c - 在自引用结构上使用 sizeof(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43298260/