2013-03-22 77 views
0

讓我有下面的程序。我想在運行時將值賦給枚舉成員。我該怎麼做?如何在運行時爲枚舉成員賦值?

typedef enum test{ 
    a, b 
}test; 
typedef struct abc{ 
    test Test; 
}abc; 
int main(){ 

    abc ab; 
    ab.Test.a = 5;//Throwing an error as "Expression must have class type" 
    return 0; 
} 

請幫幫我。

+2

如果您收到「表達必須有一流的類型」,你不這樣做C.(C有沒有類) – Mat 2013-03-22 06:47:45

+0

呃,你知道什麼樣的[枚舉](HTTP:// EN .wikipedia.org/wiki/Enumerated_type)是? (提示,常量) – tjameson 2013-03-22 06:47:50

+0

我得到'錯誤:請求成員'a'的東西不是結構或聯合'。這更有幫助嗎? – tjameson 2013-03-22 06:51:12

回答

3

enum只是給的名字一定constants,爲了清楚起見方式。

它很有用,因爲與defines相反,您爲變量設置的名稱(通常)不會被編譯器丟棄,因此您可以在使用調試器瀏覽程序時看到它們。

如果要重新組合變量併爲其設置值,請改爲使用structures

3

首先枚舉值是常量,因此,它們不能在代碼中隨後更改。

其次,我不知道你正在嘗試做的..

1

你的意思是這個嗎?

typedef struct test{ 
    int a, b; 
} test; 
typedef struct abc{ 
    test Test; 
} abc; 

int main(){ 

    abc ab; 
    ab.Test.a = 5; 
    return 0; 
}