0

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];
5
  • 3
    Did you mean Word_t array[a]; (without the struct)? Commented May 7, 2017 at 13:05
  • 1
    I propose to remove typedef from "Beginner's C". If grown up they will drop it anyway. :-) Commented May 7, 2017 at 13:12
  • @alk - Careful, such rhetoric can start a flame war (from experience, since I share your view :) ) Commented May 7, 2017 at 13:16
  • I am in my first year in college. I just learned to do it with typedef, don't really know another way. But thanks for your help, I just had to remove "struct". Thank you Commented May 7, 2017 at 13:19
  • Just do it straight forward: Type definition: struct Word {...}; Variable definition: struct Word words[42]; Commented May 7, 2017 at 13:20

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Rookie mistake. Thank you : )
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.