1

我在文件“vector.c”中有这个结构:

struct Vector
{
int * m_items;
int m_size;
int m_extSize;
int m_numItems;
};

在“main.c”上,我试图检查某个向量的值 m_items 是否为 NULL :

if (! vec->m_items)
        printf("not fail\n");

我在使用值初始化“vec”之后执行此操作 - 向量有 1 个值(我检查了它)。但是,gcc 正在为上面的行写一个错误:

Error: dereferencing pointer to incomplete type

为什么?

4

1 回答 1

2

您必须将整个定义Vector移至头文件并将其包含在 vector.c 和 main.c 中。

添加一个typedef struct Vector Vector;是不够的。这只是告诉编译器有一个类型Vector并且在其他地方定义,所以它是不完整的。它允许您声明指向它的指针,因为它不需要知道它拥有哪些成员来分配指针。所有指针的大小相同。

于 2013-01-08T19:05:41.867 回答