C语言 '{' 标记前的预期表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13275751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
expected expression before '{' token
提问by txcotrader
I am getting: "error: expected expression before '{' token" for the line I've commented before. If the struct is already defined why would it need a "{" before token. Thanks for any help you can provide.
对于我之前评论过的行,我收到:“错误:'{' 标记之前的预期表达式”。如果结构已经定义,为什么在标记前需要一个“{”。感谢您的任何帮助,您可以提供。
struct sdram_timing {
u32 wrdtr;
u32 clktr;
};
int calibration(void);
unsigned char read_i2c_cal(void);
static unsigned int eepcal[15];
main() {
DQS_autocalibration();
}
int calibration(void)
{
struct sdram_timing scan_list[30];
read_i2c_cal();
if(eepcal[0] == 0){
scan_list = {{eepcal[1], eepcal[2]}, {-1, -1}}; // <-- PROBLEM LINE
}
else {
//foo
}
return 0;
}
unsigned char read_i2c_cal(void) {
eepcal[0] = 0;
eepcal[1] = 02;
eepcal[2] = 03;
}
回答by Mike
The error is because you can't assign an array that way, that only works to initialize it.
错误是因为您不能以这种方式分配数组,它只能用于初始化它。
int arr[4] = {0}; // this works
int arr2[4];
arr2 = {0};// this doesn't and will cause an error
arr2[0] = 0; // that's OK
memset(arr2, 0, 4*sizeof(int)); // that is too
So applying this to your specific example:
因此,将其应用于您的具体示例:
struct sdram_timing scan_list[30];
scan_list[0].wrdtr = 0;
scan_list[0].clktr = 0;
or you could use memset the same way, but instead of sizeof(int) you need size of your structure. That doesn't always work... but given your structure, it will.
或者您可以以相同的方式使用 memset,但是您需要结构的大小而不是 sizeof(int)。这并不总是有效......但考虑到你的结构,它会。
回答by AnT
Arrays in C language are not assignable. You can't assign anything to the entire array, regardless of what syntax you use. In other words, this
C 语言中的数组是不可赋值的。无论您使用什么语法,您都无法为整个数组分配任何内容。换句话说,这
scan_list = { { eepcal[1], eepcal[2] }, {-1, -1} };
is not possible.
不可能。
In C89/90 you'd have to spell out your assignments line by line
在 C89/90 中,您必须逐行拼出您的作业
scan_list[0].wrdtr = eepcal[1];
scan_list[0].clktr = eepcal[2];
scan_list[1].wrdtr = -1;
scan_list[1].clktr = -1;
In modern C (post-C99) you can use compound literals to assign entire structs
在现代 C(C99 之后)中,您可以使用复合文字来分配整个结构
scan_list[0] = (struct sdram_timing) { eepcal[1], eepcal[2] };
scan_list[1] = (struct sdram_timing) { -1, -1 };
Finally, in modern C you can use memcpyand compound literals to copy data to the array
最后,在现代 C 中,您可以使用memcpy和复合文字将数据复制到数组
memcpy(scan_list, (struct sdram_timing[]) { { eepcal[1], eepcal[2] }, {-1, -1} },
2 * sizeof *scan_list);
The last variant, albeit not very elegant, is the closest way to "emulate" array assignment.
尽管不是很优雅,但最后一个变体是“模拟”数组分配的最接近方法。
回答by Jim Buck
You can only use an initializer list in the declaration of the variable, not after the fact.
您只能在变量声明中使用初始化列表,而不能在事后使用。
回答by texasbruce
Initializer list can only be used to initialize an array. You cannot use it afterwards.
初始化列表只能用于初始化数组。之后您将无法使用它。
However if you use GCC, you can use Compound Literalextension:
但是,如果您使用GCC,则可以使用Compound Literal扩展名:
scan_list = (struct sdram_timing[30]){{eepcal[1], eepcal[2]}, {-1, -1}};
You might need to change scan_listtype to be struct sdram_timing *
您可能需要将scan_list类型更改为struct sdram_timing *

