Declare an array of structures

旧巷老猫 提交于 2019-12-13 21:50:25

问题


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

回答1:


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. There is no struct Word_t, only a Word_t type defined.

The tag namespace (where the names that you use after struct/union/enum reside) is separate from the global namespace (where file scope typedefed names reside).

Many programmers feel that lugging around a struct tag type name is cumbersome, and you should always do a typedef for the struct name. Others feel this is an abuse of typedef-ing, and that the keyword conveys a lot of meaning; to them the typedef is syntactic sugar without any real abstraction.

Whichever way you choose to write you code, stick to it, and don't confuse the two namespaces.




回答2:


You are using typedef in the first place hence you are not supposed to write the struct anymore . 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



来源:https://stackoverflow.com/questions/43831826/declare-an-array-of-structures

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