I have a question about combining typedef and struct
I want to have a struct st
, containing an enum
e
with elements ie {A,B,C}. Later in my code, I want be able to write:
st.e=A;
One possibility is to write following code into a header file
typedef enum _enumDef{A,B,C} enumDef;
typedef struct _structDef{
enumDef e;
...
}structDef;
and then in my c-file I type structDef st
and st.e=A
Now my question is, can I also write something like:
typedef struct _structDef{
typedef enum _enumDef{A,B,C} enumDef e;
...
}structDef;
? This one above doesn't work. But it is possible, to write
tyedef struct _structDef{
enum _enumDef{A,B,C} e;
...
}structDef;
but then I could not write st.e=A
because the enum isn't known as global argument. Instead I have to write st.e=st.A
So my question is, is there any possibility, to include the typedef into the struct? I think, it looks nicer and it it easier to understand, from which context the enum is from. Or this this total unusual and I should clear it from my mind :D ? Thanks
I will answer this question
Now my question is, can I also write something like:
typedef struct _structDef{ typedef enum _enumDef{A,B,C} enumDef e; ... }structDef; ?
I will give you the similarity so you can see the problem
when you want to create an int
you write
int a,b,c;
so int
is the type and a
b
c
are variable
typedef int a,b,c;
Now you can create other variable with type int
via the tag a
or b
or c
so
a variable1; //is the same as ==> int variable1;
b variable2; //is the same as ==> int variable2;
c variable3; //is the same as ==> int variable3;
let's return to our problem when take a look at this below to see the syntax and the similarity
enum _enumDef{A,B,C} e;
|------------------|
|
|------------------|
int e;
In order to create many varibales you need to add commas like in int
enum _enumDef{A,B,C} a,b,c;
|------------------| |----|
| |---->variables;
|------------------| |----|
int a,b,c;
the problem here
typedef enum _enumDef{A,B,C} enumDef e;
|--------|
|-------> no comma (ERROR)
is there is no comma between the two SAME tags ( e
and enumDef
) and that's why it generates the problem !!
In order to make it work just add the comma between the same two tags e
and enumDef
of structure enum _enumDef {...}
:
typedef enum _enumDef{A,B,C} enumDef, e;
is there any possibility, to include the typedef into the struct?
storage class specifiers is not allowed within a structure as per C standard.
So you can't have a typedef
member within structure
If you think of the {}
in the struct as defining a scope it tells you that everything within those brackets is local to the struct and putting a typedef in there wouldn't add much even if the C language allowed you to do it.
(Inventing a conceptual namespace implementation might give some kind of structDef::enumDef
syntax, but that's not part of C.)
If you need to use the enumeration type outside of the struct then it should be typedef'd outside the struct. The first example you give is the correct usage.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.