简体   繁体   English

c将typedef放入struct

[英]c put typedef into struct

I have a question about combining typedef and struct 我对合并typedef和struct有疑问

I want to have a struct st , containing an enum e with elements ie {A,B,C}. 我想要一个结构st ,其中包含带有元素的enum e {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 然后在我的c文件中键入structDef stst.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. 但是后来我无法写st.e=A因为该枚举不被称为全局参数。 Instead I have to write st.e=st.A 相反,我必须写st.e=st.A

So my question is, is there any possibility, to include the typedef into the struct? 所以我的问题是,是否有可能将typedef包含在结构中? 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 ? 还是这完全不寻常,我应该从我的脑海中清除它: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你写

int a,b,c;

so int is the type and a b c are variable 所以int是类型,而a b c是变量

typedef int a,b,c;

Now you can create other variable with type int via the tag a or b or c so 现在,您可以通过标签 abc创建其他类型为int变量,因此

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 为了创建许多变量,您需要添加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 !! 两个SAME标记( eenumDef )之间没有逗号,这就是它产生问题的原因!

In order to make it work just add the comma between the same two tags e and enumDef of structure enum _enumDef {...} : 为了使其工作,只需在结构enum _enumDef {...}相同两个标签 eenumDef之间添加逗号:

typedef enum _enumDef{A,B,C} enumDef, e;

is there any possibility, to include the typedef into the struct? 有没有可能将typedef包含到结构中?

storage class specifiers is not allowed within a structure as per C standard. 根据C标准,在结构内不允许使用存储类说明符。

So you can't have a typedef member within structure 所以你不能在结构中有一个typedef成员

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. 如果您认为结构中的{}定义了范围,它将告诉您括号内的所有内容都是该结构的本地内容,即使C语言允许您在其中放置typedef也不会增加太多。

(Inventing a conceptual namespace implementation might give some kind of structDef::enumDef syntax, but that's not part of C.) (发明一个概念上的名称空间实现可能会提供某种structDef::enumDef语法,但这不是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. 您提供的第一个示例是正确的用法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM