Ivan Rosa
Ivan Rosa

Reputation: 1

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

Upvotes: 0

Views: 404

Answers (2)

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.

Upvotes: 3

Deepak Sharma
Deepak Sharma

Reputation: 458

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

Upvotes: 0

Related Questions