learner
learner

Reputation: 1321

typedef may be undeclared or ambiguous

In a header file I have a single struct:

#ifndef _MY_STRUCT_
#define _MY_STRUCT_
struct myStruct{
   char c1[1];
   char c2[10];
   char c3[10];
}

typedef myStruct MYSTRUCT;
#endif

And in another header file this struct:

#include "my_struct.h"
struct another_struct{
    int val1;
    MYSTRUCT strct;
};

When I compile this code I receive the following message:

The text "strct" is unexpected. MYSTRUCT may be undeclared or ambiguous.

For me both structures are fine. I can't understand why compiler is complaining about this.

Thanks for help.

Upvotes: 3

Views: 1237

Answers (2)

Itay Sela
Itay Sela

Reputation: 974

here's how i think it should be done

#ifndef _MY_STRUCT_
#define _MY_STRUCT_
typedef struct {
   char c1[1];
   char c2[10];
   char c3[10];
} MYSTRUCT;

#endif

and the other file stays the same.

Upvotes: 5

Some programmer dude
Some programmer dude

Reputation: 409452

You should have gotten an error for the typedef as well, as you're missing the struct keyword there. It should be

typedef struct myStruct MYSTRUCT;
//      ^^^^^^
// Note the `struct` keyword here

And the missing semicolons at the end of the structures doesn't make the compiler any happier.

Upvotes: 9

Related Questions