C语言 C 编译错误:数组类型具有不完整的元素类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21080744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:36:47  来源:igfitidea点击:

C Compile Error: array type has incomplete element type

carraysstructcompiler-errors

提问by JonAthan LAm

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

我相信问题在于我错误地声明了结构数组。当我查到它时,这似乎就是你声明它的方式。

回答by haccks

struct NUMBER array[99999];  

should be

应该

NUMBER array[99999];  

because you already typedefed your struct.

因为你已经typedef编辑了你的结构。



EDIT: As OP is claiming that what I suggested him is not working, I compiled this test code and it is working fine:

编辑:由于 OP 声称我建议他的内容不起作用,我编译了此测试代码并且运行正常:

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

查看运行代码

回答by glglgl

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_struct1is in the structnamespace and must always be preceded with structin order to be used. (That is one major difference between C and C++.)

您可以同时使用它们,但它们anonymous_struct1位于struct命名空间中,并且必须始终以 开头struct才能使用。(这是 C 和 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];