2013-12-10 80 views
0

我有以下代碼:如何枚舉類型搜索用C

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 

回答

0

enum S IN C是int類型始終。

您可以測試它反對任何int

if (node->structAccessor.MyType == Type_1) 

雖然他們在switch聲明

switch (node->structAccessor.MyType) { 
    case Type_1: 
    ... 
    case Type_2: 
    ... 
    case Type_3: 
    ... 
    default: // error 
} 
常用