Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C: "array type has incomplete element type" when using array of struct without typedef

Problem: The following code snippet compiles well (where both struct types are typedefed):

typedef struct {
    int a;
    float b;
} member_struct;

typedef struct {
    int a;
    double b;
    member_struct c;
} outside_struct;

outside_struct my_struct_array[4];

However, if the typedef of the "outside_struct" is dropped:

typedef struct {
    int a;
    float b;
} member_struct;

struct {
    int a;
    double b;
    member_struct c;
} outside_struct;

struct outside_struct my_struct_array[4];

I get the error:
"array type has incomplete element type 'struct outside_struct'".
And if I also drop the typedef of "member_struct", I get an extra error:
"field 'c' has incomplete type"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Question: Why it happens? Is using typedef strictly necessary here? In my code, I otherwise never use typedef for structure types, so I am looking for a way to avoid that, if possible.

>Solution :

In this declaration

struct {
    int a;
    double b;
    member_struct c;
} outside_struct;

there is declared the object outside_struct of unnamed structure type. Neither structure with the name struct outside_struct is declared.

So the compiler issues an error in this declaration of an array

struct outside_struct my_struct_array[4];

because in this declaration there is introduced the type specifier struct outside_struct that is not defined. That is in this declaration the type specifier struct outside_struct is an incomplete type.

You may not declare an array with an incomplete element type.

Instead of declaring the object outside_struct of an unnamed structure you need to declare a structure with the same tag name as

struct  outside_struct {
    int a;
    double b;
    member_struct c;
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading