Daniel Nunes Daniel Nunes - 1 year ago 82
C Question

C struct, incomplete field type

A snippet of my struct declaration:

struct record{
char type[4];
uint32_t data_size;
uint32_t flags;
uint32_t id;
uint32_t revision;
uint16_t version;
uint16_t unknown;
uint8_t *data;

struct group;

union subgroup_record{
int type;
struct group subgroup;
struct record record;

struct record_list{
union subgroup_record subgroup_record;
struct record_list *next;

struct group{
char type[4];
uint32_t group_size;
uint8_t label[4]; // depends on group_type
int32_t group_type;
uint16_t stamp;
uint16_t unknown1;
uint16_t version;
uint16_t unknown2;
struct record_list record_list;

struct group_list{
struct group group;
struct group_list *next;

struct plugin{
struct record header;
struct group_list top_groups;

The compiler gives me this error:

error: field ‘subgroup’ has incomplete type

Forward declaring the struct didn't help, typedef'ing the struct and changing the declarations also didn't help and I'd rather not have pointers everywhere (for a beginner the allocating and freeing memory is a bit scary)

Any solution for this?


Answer Source

You have a circular recursive definition.

The type struct group contains the type struct record_list, which contains the type union subgroup_record, which contains the type struct group

A type cannot contain itself.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download