gpt4 book ai didi

c 将 typedef 放入结构中

转载 作者:太空宇宙 更新时间:2023-11-04 07:11:34 25 4
gpt4 key购买 nike

我有一个关于组合 typedef 和 struct 的问题

我想要一个结构 st,包含一个 enum e 元素,即 {A,B,C}。稍后在我的代码中,我希望能够编写:

st.e=A;

一种可能是将以下代码写入头文件

typedef enum _enumDef{A,B,C} enumDef;
typedef struct _structDef{
enumDef e;
...
}structDef;

然后在我的 c 文件中键入 structDef stst.e=A

现在我的问题是,我是否也可以这样写:

typedef struct _structDef{
typedef enum _enumDef{A,B,C} enumDef e;
...
}structDef;

?上面这个不行。但是可以这样写

tyedef struct _structDef{
enum _enumDef{A,B,C} e;
...
}structDef;

但后来我无法编写 st.e=A 因为枚举不被称为全局参数。相反,我必须编写 st.e=st.A

所以我的问题是,是否有可能将 typedef 包含到结构中?我认为,枚举来自哪个上下文,它看起来更好,也更容易理解。或者这完全不寻常,我应该把它从我的脑海中清除 :D ?谢谢

最佳答案

is there any possibility, to include the typedef into the struct?

根据 C 标准,结构中不允许使用存储类说明符。

所以你不能在结构中有一个typedef成员

关于c 将 typedef 放入结构中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28047197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com