C Compile Error: array type has incomplete element type

寵の児 提交于 2021-02-08 13:16:38

问题


#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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/21080744/c-compile-error-array-type-has-incomplete-element-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!