我正在尝试转发声明 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/