gob*_*ien 3 c coding-style typedef forward-declaration
我想使用尚未定义的 typedef 结构,但它是稍后定义的。有没有类似结构体原型的东西?
文件容器.h
// i would place a sort of struct prototype here
typedef struct
{
TheType * the_type;
} Container;
Run Code Online (Sandbox Code Playgroud)
文件 thetype.h
typedef struct {......} TheType;
Run Code Online (Sandbox Code Playgroud)
文件main.c
#include "container.h"
#include "thetype.h"
...
Run Code Online (Sandbox Code Playgroud)
在container.h中:
struct _TheType;
typedef struct _TheType TheType;
Run Code Online (Sandbox Code Playgroud)
比在 thetype.h 中:
struct _TheType { ..... };
Run Code Online (Sandbox Code Playgroud)
替换这一行:
// i would place a sort of struct prototype here
Run Code Online (Sandbox Code Playgroud)
用这些行:
struct TheType;
typedef struct TheType TheType;
Run Code Online (Sandbox Code Playgroud)
TheType由于您需要在定义类型之前定义类型Container,因此您必须使用类型的前向声明TheType- 为此,您还需要 struct 的前向声明TheType。
那么你就不会TheType像这样定义 typedef:
typedef struct {......} TheType;
Run Code Online (Sandbox Code Playgroud)
但你将定义 struct TheType:
struct {......};
Run Code Online (Sandbox Code Playgroud)