[英]two structs that refer to each other
How can I have two different structs that refer to each other?我怎样才能有两个相互引用的不同结构? One holds a pointer to the other and I also have a forward declaration:
一个持有指向另一个的指针,我也有一个前向声明:
struct json_array_t;
struct json_array_entry_t {
enum json_type type;
union {
bool boolean;
long long integer;
double floating;
char* string;
struct json_array_t array;
};
};
struct json_array_t {
struct json_array_entry_t* entries;
size_t len, cap;
};
I am getting these errors:我收到这些错误:
error: field ‘array’ has incomplete type
27 | struct json_array_t array;
You must first define struct json_array_t
and then struct json_array_entry_t
.您必须先定义
struct json_array_t
,然后再定义 struct struct json_array_entry_t
。
When you now define in struct json_array_entry_t
an occurrence of json_array_t
it and all its members are fully known to the compiler.当您现在在
struct json_array_entry_t
中定义json_array_t
的出现时,它及其所有成员对编译器都是完全已知的。
You can define json_array_entry_t
before json_array_t
since you will just use it as a pointer inside json_array_t
您可以在
json_array_t
之前定义json_array_entry_t
,因为您只需将其用作json_array_t
内的指针
#include <stdio.h>
#include <stdbool.h>
// just an example
enum json_type {
_BOOL, _INT, _DOUBLE, _STRING, _ARRAY
};
struct json_array_entry_t;
struct json_array_t {
struct json_array_entry_t* entries;
size_t len, cap;
};
struct json_array_entry_t {
enum json_type type;
union {
bool boolean;
long long integer;
double floating;
char* string;
struct json_array_t array;
};
};
int main() {
// some coding
return 0;
}
The compiler needs to know the exact size of struct json_array_t
to determine how much memory it needs to allocate for the union
member inside of the structure json_array_entry_t
and with that how much memory it needs to allocate for an object of the structure json_array_entry_t
in whole. The compiler needs to know the exact size of
struct json_array_t
to determine how much memory it needs to allocate for the union
member inside of the structure json_array_entry_t
and with that how much memory it needs to allocate for an object of the structure json_array_entry_t
in whole.
A simple forward declaration of struct json_array_t
, as you did, is not sufficient in this case.在这种情况下,像您所做的那样简单的
struct json_array_t
前向声明是不够的。
Place the definition of struct json_array_t
放置
struct json_array_t
的定义
struct json_array_t {
struct json_array_entry_t* entries;
size_t len, cap;
};
before the definition of struct json_array_entry_t
.在
struct json_array_entry_t
的定义之前。 In this way the compiler knows the size of the structure json_array_t
and you can also spare the forward declaration of it.通过这种方式,编译器知道结构
json_array_t
的大小,您也可以省去它的前向声明。
Since entries
is only a pointer you don't need a forward-declaration for json_array_entry_t
.由于
entries
只是一个指针,因此您不需要json_array_entry_t
的前向声明。
struct json_array_t {
struct json_array_entry_t* entries;
size_t len, cap;
};
struct json_array_entry_t {
enum json_type type;
union {
bool boolean;
long long integer;
double floating;
char* string;
struct json_array_t array;
};
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.