【问题标题】:C Compile Error: array type has incomplete element typeC编译错误:数组类型的元素类型不完整
【发布时间】:2014-01-31 13:50:02
【问题描述】:
#include <stdio.h>    
typedef  struct
    {       
        int   num ;
    } NUMBER ;

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

我得到一个编译错误:

error: array type has incomplete element type

我认为问题在于我错误地声明了结构数组。我查的时候你好像是这样声明的。

【问题讨论】:

    标签: c arrays struct compiler-errors


    【解决方案1】:
    struct NUMBER array[99999];  
    

    应该是

    NUMBER array[99999];  
    

    因为您已经 typedefed 您的结构。


    编辑:由于 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;
    }  
    

    请参阅running code。

    【讨论】:

    • 或 struct NUMBER{int num;}; 没有 typedef
    • NUMBER 数组[99999];给我更多的错误。其中一项声明:NUMBER 未声明。
    • 没有。唯一的预期警告/错误应该是:[Warning] unused variable 'array' [-Wunused-variable]。请参阅此处的代码:ideone.com/JIjxy8
    • error: 'NUMBER' undeclared (first use in this function) error: (每个未声明的标识符只报告一次错误:对于它出现的每个函数。) error: expected ';'在'数组'之前
    • 你使用的是哪个编译器?
    【解决方案2】:

    你有

    typedef  struct
        {       
            int   num ;
        } NUMBER ;
    

    这是

    的简写
    struct anonymous_struct1
        {       
            int   num ;
        };
    typedef struct anonymous_struct1 NUMBER ;
    

    您现在有两种等效类型:

    struct anonymous_struct1
    NUMBER
    

    您可以同时使用它们,但anonymous_struct1 位于struct 命名空间中,并且必须始终以struct 开头才能使用。 (这是 C 和 C++ 之间的主要区别之一。)

    所以要么你就这样做

    NUMBER array[99999];
    

    或者你定义

    typedef  struct number
        {       
            int   num ;
        } NUMBER ;
    

    或者干脆

    struct number
        {       
            int   num ;
        };
    

    然后做

    struct number array[99999];
    

    【讨论】:

    • 结构编号 { int num ; };
    • 我试过你的最后一个方法,但它仍然说:错误:数组类型有不完整的元素类型
    • 我也遇到了这个错误。我的结构包含自己的结构。所以我想知道它是否还没有定义?
    • 这是因为你不能定义一个包含自身大小数组的大小数组,这显然是有道理的:P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 2018-10-24
    • 1970-01-01
    • 2022-06-25
    相关资源
    最近更新 更多