C Compilation error: array type has an incomplete element type

#include <stdio.h> typedef struct { int num ; } NUMBER ; int main(void) { struct NUMBER array[99999]; return 0; } 

I get a compilation error:

 error: array type has incomplete element type 

I believe the problem is that I am declaring the structure array incorrectly. It seems like this is how you declare it when I looked through it.

+6
source share
2 answers
 struct NUMBER array[99999]; 

it should be

 NUMBER array[99999]; 

because you already typedef created your structure.


EDIT: Since the OP claims that what I suggested to him does not work, I compiled this test code and it works fine:

 #include <stdio.h> typedef struct { int num ; } NUMBER ; int main(void) { NUMBER array[99999]; array[0].num = 10; printf("%d", array[0].num); return 0; } 

See running code .

+13
source

You have

 typedef struct { int num ; } NUMBER ; 

which is short for

 struct anonymous_struct1 { int num ; }; typedef struct anonymous_struct1 NUMBER ; 

You now have two equivalent types:

 struct anonymous_struct1 NUMBER 

You can use both of them, but anonymous_struct1 is in the struct namespace and there must always be a struct for this. (This is one significant difference between C and C ++.)

So either you just do

 NUMBER array[99999]; 

or you define

 typedef struct number { int num ; } NUMBER ; 

or simply

 struct number { int num ; }; 

and then do

 struct number array[99999]; 
+3
source

All Articles