C语言 “'{' 标记之前的预期表达式”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20086905/
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 user2930701
So I keep on running into this issue when I try assigning values to a int array. I read this one expected expression before '{' token, but I am still confused on why it is showing up in my code. I have a feeling I am initializing and declaring the array incorrectly and that's why it is giving my issues.
因此,当我尝试为 int 数组赋值时,我不断遇到这个问题。我在 '{' token 之前阅读了这个预期的表达式,但我仍然对它为什么出现在我的代码中感到困惑。我有一种感觉,我正在错误地初始化和声明数组,这就是为什么它给我的问题。
So, before main () I am declaring some group of global variables (yes I know this is dangerous, but required for my purpose). With that group of global variables I also want to declare an double array of size 3
所以,在 main () 之前,我声明了一些全局变量(是的,我知道这是危险的,但我的目的是必需的)。使用这组全局变量,我还想声明一个大小为 3 的双数组
double rob_size, rob_tilt;
double rob_leftcolor [3];
double rob_rightcolor [3];
Then in the main function, I am initializing the variables and arrays
然后在主函数中,我正在初始化变量和数组
rob_size = 1.0;
rob_tilt = 0.0;
rob_leftcolor [3] = {1.0, 0.0, 0.0};
rob_rightcolor [3] = {0.0, 1.0, 0.0};
However, I am getting the error message "Expected expression before ' { ' token" at.
但是,我收到错误消息“'{'令牌之前的预期表达式”。
First of all, what does that error message mean? Secondly, is that message coming up because I am initializing and declaring the arrays incorrectly?
首先,该错误消息是什么意思?其次,出现该消息是因为我错误地初始化和声明数组吗?
Thanks
谢谢
回答by Charlie Burns
Best to do the init'ing at declaration time:
最好在声明时进行初始化:
double rob_size = 1.0;
double rob_tilt = 0.0;
double rob_leftcolor [3] = {1.0, 0.0, 0.0};
double rob_rightcolor [3] = {0.0, 1.0, 0.0};
Only the arrays needto be done that way, but it's best to do them all the same way.
只有数组需要以这种方式完成,但最好以相同的方式完成它们。
Your alternative is
你的选择是
rob_leftcolor[0] = 1.0;
rob_leftcolor[1] = 0.0;
rob_leftcolor[2] = 0.0;
回答by DerStrom8
Charlie Burns is correct, it's always better to initialize the arrays with actual values. However, using the code you supplied, once you declare the array you can only set specific elements:
Charlie Burns 是正确的,用实际值初始化数组总是更好。但是,使用您提供的代码,一旦声明数组,您就只能设置特定元素:
double x[3];
x[0] = 1.1;
x[1] = 2.2;
x[2] = 3.3;
As you can see, in order to set the variable you use the number inside the brackets corresponding to the element you are trying to set. you cannot set it all at once after declaring the array.
如您所见,为了设置变量,您可以使用与您要设置的元素相对应的括号内的数字。您不能在声明数组后一次全部设置。
回答by raof01
After looking at the answers and comments, I think there are a few more things to clarify:
看了大家的回答和评论,我觉得还有几点需要澄清:
- If variables are defined / declared outside any function body in
a compilation unit, you must specify the storage class , e.g.
static - These variables will be put to BSS section of you executable file. And you'd better initialize then.
- When defining an
array, you could use initializer
{...}to initialize it. But you cannot use it in assignment statement. double rob_leftcolor[3] = {1.0, 0.0, 0.0}is a definition, whilerob_leftcolor[3] = {1.0, 0.0, 0.0}is an assignment, so you can not use initializer here.- Please make sure you know the differences between declaration and definition in C.
- 如果在编译单元中的任何函数体之外定义/声明变量,则必须指定存储类,例如
static - 这些变量将被放入可执行文件的 BSS 部分。然后你最好初始化。
- 定义数组时,可以使用初始化
{...}程序对其进行初始化。但是你不能在赋值语句中使用它。 double rob_leftcolor[3] = {1.0, 0.0, 0.0}是一个定义,rob_leftcolor[3] = {1.0, 0.0, 0.0}而是一个赋值,所以你不能在这里使用初始化器。- 请确保您知道 C 中声明和定义之间的区别。
For storage class, consider the case below, without static, you are actually defining global variables:
对于存储类,请考虑以下情况,没有static,您实际上是在定义全局变量:
// a1.c
static double rob_size, rob_tilt;
rob_leftcolor [3] = {1.0, 0.0, 0.0};
rob_rightcolor [3] = {0.0, 1.0, 0.0};
int main(int argc, char** argv) {
rob_size = 1.0;
rob_tilt = 0.0;
return 0;
}
// a2.c
rob_leftcolor [3] = {1.0, 0.0, 0.0};
Then compile & link them:
然后编译并链接它们:
$ gcc -c a1.c
a1.c:2:1: warning: data definition has no type or storage class [enabled by default]
a1.c:3:1: warning: data definition has no type or storage class [enabled by default]
$ gcc -c a2.c
a2.c:1:1: warning: data definition has no type or storage class [enabled by default]
$ gcc -o a a1.o a2.o
a2.o:(.data+0x0): multiple definition of `rob_leftcolor'
a1.o:(.data+0x0): first defined here
collect2: error: ld returned 1 exit status

