Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C Compile Error: array type has incomplete element type

#include <stdio.h>    
typedef  struct
    {       
        int   num ;
    } NUMBER ;

    int main(void)
    {   
        struct NUMBER array[99999];
        return 0;
    }

I'm getting a compile error:

error: array type has incomplete element type

I believe the problem is that I'm declaring the array of struct incorrectly. It seems like that's how you declare it when I looked it up.

like image 257
JonAthan LAm Avatar asked Jan 12 '14 21:01

JonAthan LAm


2 Answers

struct NUMBER array[99999];  

should be

NUMBER array[99999];  

because you already typedefed your struct.


EDIT: As OP is claiming that what I suggested him is not working, I compiled this test code and it is working 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 the running code.

like image 143
haccks Avatar answered Nov 01 '22 13:11

haccks


You have

typedef  struct
    {       
        int   num ;
    } NUMBER ;

which is a shorthand for

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

You have now two equivalent types:

struct anonymous_struct1
NUMBER

You can use them both, but anonymous_struct1 is in the struct namespace and must always be preceded with struct in order to be used. (That is one major 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];
like image 37
glglgl Avatar answered Nov 01 '22 13:11

glglgl