我有以下代码:
typedef enum Types{
Type_1, Type_2, Type_3
} MyTypes;
typedef union Tree{
struct {
int MyType;
}structAccessor;
} MyTree;
我正在创建这样的树:
MyTree* node(MyTypes MyType).......//folowwing is unnecessary
我想知道如何找到我的树的类型,如下所示:
if(node->structAccessor.MyType == MyTypes[2]) //if MyType is Type_2, i want to compare this, thanks so much
最佳答案
C 中的
enum
始终为 int
类型。
您可以针对任何 int
进行测试,例如
if (node->structAccessor.MyType == Type_1)
尽管它们常用于 switch
语句
switch (node->structAccessor.MyType) {
case Type_1:
...
case Type_2:
...
case Type_3:
...
default: // error
}
关于c - 如何在C中搜索枚举类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20500366/