Re: [問題] 陣列給名問題

看板C_and_CPP作者 (:))時間13年前 (2011/02/01 17:02), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《lucifer1C1 (驕傲)》之銘言: : 使用的是C語言 : 目前有一個已知字串 : char fruit_name[10] = "Apple"; : 和一個已知interger : int amount = 32; : 我想問的是我想要利用上面的字串跟數值創建出一個陣列..... : 利用"fruit_name[10]"的值跟"amount"的值創造出 int Apple[32]一樣的陣列.... : 並且利用"fruit_name[10]"的值去寫入Apple[n] (0<n<31) : 程式碼該如何寫? : ===================================================== : C語言苦手中...... 可以利用 macro 來達成... #include <stdio.h> #define create_array(x) int x[x##_amount] #define get_name(x) #x enum amounts { apple_amount = 32, orange_amount = 10 }; int main() { int i; char fruits[2][10] = { get_name(apple), get_name(orange) }; create_array(apple); create_array(orange); printf("fruits[0] = \"%s\"\n", fruits[0]); printf("fruits[1] = \"%s\"\n", fruits[1]); printf("apple array's size = %d\n", sizeof(apple)/sizeof(int)); printf("orange array's size = %d\n", sizeof(orange)/sizeof(int)); return 0; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.116.14.188 ※ 編輯: chubiei 來自: 122.116.14.188 (02/01 17:17)

02/01 17:18, , 1F
方向錯了
02/01 17:18, 1F
文章代碼(AID): #1DHymxsR (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1DHymxsR (C_and_CPP)