简体   繁体   中英

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.

struct NUMBER array[99999];  

should be

NUMBER array[99999];  

because you already typedef ed 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 .

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM