可能重複:
Easy way to use variables of enum types as string in C?C:地圖串ENUM
有沒有用戶輸入的字符串轉換爲一個ENUM值的任何優雅的方式是直C,除了手工方式。
調用一個函數,它接受一個ENUM作爲參數的簡化示例:
enum = {MONDAY,TUESDAY,WEDNESDAY};
...
//Get user to enter a day of the week from command line
...
//Set the work day according to user input
if (strcmp(user_input,"MONDAY")==0){
SET_WORK_DAY(MONDAY);
} else if (strcmp(user_input,"TUESDAY")==0){
SET_WORK_DAY(TUESDAY);
}
...
由於
我想你的意思是'strcmp'而不是'strcpy'? – cnicutar
@bobbymcr這不是一個騙局;這個問題是相似的,但它是相反的方式(字符串枚舉的枚舉爲字符串)。 – 2011-12-27 09:46:02
[在C中使用枚舉類型的變量作爲字符串的簡單方法](http://stackoverflow.com/questions/147267/easy-way-to-use-variables-of-enum-types-as-string-in -c)用於獲取ENUM的名稱,使用ENUM值作爲輸入。我需要使用ENUM名稱作爲輸入來獲取ENUM值。 – squater