Fab*_*cio 4 c struct typedef declaration incomplete-type
在.h 中:
typedef struct token_t TOKEN;
Run Code Online (Sandbox Code Playgroud)
在.c 中:
#include "token.h"
struct token_t
{
char* start;
int length;
int type;
};
Run Code Online (Sandbox Code Playgroud)
在main.c 中:
#include "token.h"
int main ()
{
TOKEN* tokens; // here: ok
TOKEN token; // here: Error: incomplete type is not allowed
// ...
}
Run Code Online (Sandbox Code Playgroud)
我在最后一行中得到的错误:
错误:不允许不完整的类型
怎么了?
在主模块中没有定义结构。您必须将其包含在头文件中,编译器不知道要为此定义分配多少内存
TOKEN token;
Run Code Online (Sandbox Code Playgroud)
因为结构的大小是未知的。大小未知的类型是不完整的类型。
例如,您可以在标题中写入
typedef struct token_t
{
char* start;
int length;
int type;
} TOKEN;
Run Code Online (Sandbox Code Playgroud)
您需要将 的定义移动struct到头文件中:
/* token.h */
struct token_t
{
char* start;
int length;
int type;
};
Run Code Online (Sandbox Code Playgroud)