我想初始化一个我做的结构,但我得到错误,我无法理解是什么导致他们。我正在与ANSI C标志GCC工作。
如果有人能帮助我了解问题所在,我将非常感激!
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};
1条答案
按热度按时间qyswt5oh1#
你所做的是一个初始化后的赋值,你不能像那样给数组赋值.
你能做的是-
这将初始化定义中的整个结构。
同时,如果你的结构体有其他没有初始值的字段,你可以使用指定的初始化器-
这会将所有其他字段初始化为相应类型的默认初始值,如指针的
NULL
,整数的0
等。如果在初始化后必须分配单个非标量字段,则可以使用
memcpy
作为你也可以使用
sizeof (instructionData.name)
,但你必须确保你的初始化器至少和字段一样长。