简体   繁体   English

声明一系列结构

[英]Declare an array of structures

I am trying to create an array of structures but it appears this bug: 我正在尝试创建结构数组,但似乎出现此错误:

"Error, array type as incomplete element type" “错误,数组类型为不完整的元素类型”

typedef struct {
char data[MAX_WORD_SIZE];     
int total_count;       
Occurrence_t reference[MAX_FILES];    
int nfiles;         
} Word_t;

struct Word_t array[a];

TL;DR TL; DR

Either change you struct definition 要么更改您的结构定义

struct Word_t {
char data[MAX_WORD_SIZE];     
int total_count;       
Occurrence_t reference[MAX_FILES];    
int nfiles;         
};

Or (and not both), the array declaration: 或(但不是全部)数组声明:

Word_t array[a];

What you did is define an un-named structure, to which you gave an alternative name with a typedef. 您要做的是定义一个未命名的结构,并为其指定了带有typedef的备用名称。 There is no struct Word_t , only a Word_t type defined. 没有struct Word_t ,仅定义了Word_t类型。

The tag namespace (where the names that you use after struct / union / enum reside) is separate from the global namespace (where file scope typedef ed names reside). 标记名称空间(在struct / union / enum之后使用的名称所驻留的位置)与全局名称空间(在文件范围内typedef名称所驻留的位置)是分开的。

Many programmers feel that lugging around a struct tag type name is cumbersome, and you should always do a typedef for the struct name. 许多程序员认为,拖曳struct tag类型名称很麻烦,您应该始终对结构名称进行typedef。 Others feel this is an abuse of typedef-ing, and that the keyword conveys a lot of meaning; 其他人则认为这是对typedef-ing的滥用,并且该关键字传达了很多含义; to them the typedef is syntactic sugar without any real abstraction. 对他们来说,typedef是语法糖,没有任何真正的抽象。

Whichever way you choose to write you code, stick to it, and don't confuse the two namespaces. 无论选择哪种方式编写代码,都要坚持下去,不要混淆这两个名称空间。

You are using typedef in the first place hence you are not supposed to write the struct anymore . 首先使用typedef,因此不再需要编写该结构。 look here 看这里

typedef struct {
    char data[MAX_WORD_SIZE];     
    int total_count;       
    Occurrence_t reference[MAX_FILES];     
    int nfiles;         
} Word_t;
Word_t array[a];

It will work 会工作的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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