问题
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 typedef
ed 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