Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C struct, incomplete field type

Tags:

c

struct

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?

Thanks!

like image 489
Daniel Nunes Avatar asked Dec 05 '25 21:12

Daniel Nunes


1 Answers

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.

like image 165
2501 Avatar answered Dec 08 '25 14:12

2501



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!