C - 前向声明和使用结构而不是指针

标签 c struct

我正在尝试转发声明 struct_A 并在 struct_B 中使用它,而不是作为指针,而是作为结构。 在 struct_B 中,我引用了 struct_B

我似乎无法正确理解 - 有帮助吗? 这是我的代码:

struct type_A_t;
typedef struct type_A_t type_A_t;

typedef struct type_B_t {
    type_A_t a;
};

struct  type_A_t{
    void (*cb)(type_B_t *b1);
    void (*cb)(type_B_t *b2);
};

我得到的错误是:

error: field 'type_A_t' has incomplete type

最佳答案

你不能这么做。但在您的示例中,由于 type_A_t 只需要知道指向 type_B_t 的指针,为什么不像这样切换它呢?

typedef struct type_B_t type_B_t;
typedef struct type_A_t type_A_t;

struct  type_A_t {
    void(*cb)(type_B_t *b1);
    void(*cb)(type_B_t *b2);
};
typedef struct type_B_t {
    type_A_t a;
};

关于C - 前向声明和使用结构而不是指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53829716/

相关文章:

在 Mac 终端中编译并运行 Sublime Text 文件

c - 轻量级 IP : Buffer not freeing

c - 如何在 Windows 上编译 memcached?

char 指针与 char 数组不同?

go - 传递与 Golang 中指定的参数类型不同的参数类型?

c - 试图理解 Knuth 的排列算法

c - 奇怪的条件检查

json - Golang - 从 JSON 响应中隐藏空结构

char* 来自 char[] 结构成员

c - 链表 : Should the type of pointer "next" and the name of struct be same?