我想初始化一個我已經做過的結構,但是出現錯誤,我無法理解造成它們的原因。 我正在使用ANSI C標誌工作在GCC上。C - 初始化數組成員結構時出錯
如果有人能幫助我理解問題所在,我將非常感謝!
typedef struct _inst {
const char *name[NUM_OF_INSTRUCTIONS];
int codes[NUM_OF_INSTRUCTIONS];
int validParam[NUM_OF_INSTRUCTIONS];
} instructions;
instructions instructionsData;
instructionsData.name[] = {"mov", "cmp", "add", "sub", "not", "clr", "lea", "inc", "dec", "jmp", "bne", "red", "prn", "jsr", "rts", "stop"};
instructionsData.codes[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
instructionsData.validParam[] = {2, 2, 2, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0};
,我從GCC得到錯誤是:
dataStructs.h:47:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
instructionsData.name[] = {"mov", "cmp", "add", "sub", "not", "clr", "lea", "inc", "dec", "jmp", "bne", "red", "prn", "jsr", "rts", "stop"};
^
dataStructs.h:47:140: warning: ISO C does not allow extra ‘;’ outside of a function [-Wpedantic]
instructionsData.name[] = {"mov", "cmp", "add", "sub", "not", "clr", "lea", "inc", "dec", "jmp", "bne", "red", "prn", "jsr", "rts", "stop"};
^
dataStructs.h:48:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
instructionsData.codes[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
^
dataStructs.h:48:82: warning: ISO C does not allow extra ‘;’ outside of a function [-Wpedantic]
instructionsData.codes[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
^
dataStructs.h:49:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
instructionsData.validParam[] = {2, 2, 2, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0};
^
dataStructs.h:49:81: warning: ISO C does not allow extra ‘;’ outside of a function [-Wpedantic]
instructionsData.validParam[] = {2, 2, 2, 2, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0};
您能否複製粘貼代碼並且不要發佈屏幕截圖? –
dupe? https://stackoverflow.com/a/41510479/2173917 –
@SouravGhosh雖然解決方案是相同的,我認爲問題是不同的。不是一個愚蠢的? –