"Expected expression before ' { ' token"

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.

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

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

4 Answers

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 need to 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; 
3

Q1: What does that error message mean?

A1: The error means that the compiler didn't expect you to assign an array to a scalar. When you specify rob_leftcolor[3] = {1.0, 0.0, 0.0};, you tell compiler, I want you to assign vector of values {1.0, 0.0, 0.0} to 4th element of array rob_leftcolor (counting starts from 0 - 0, 1, 2, 3). The compiler expects something like rob_leftcolor[0] = 1.0. As you can see, you cannot place 3 elements in the place of one. Also, you tried to assign to the fourth place in the array of three elements.

Q2: Is that message coming up because I am initializing and declaring the arrays incorrectly?

A2: You are declaring the arrays correctly but not assigning them.

Others have correctly told you the preferred way of doing it.

As an alternative, you could also use compound literal operators introduced with the C99 standard.

double rob_size = 1.0; double rob_tilt = 0.0; double *rob_leftcolor; double *rob_rightcolor; ... rob_leftcolor = (double []){1.0, 0.0, 0.0}; rob_rightcolor = (double []){0.0, 1.0, 0.0}; 

Beware, the scope of this array is only inside the function where you assigned it.

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:

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.

After looking at the answers and comments, I think there are a few more things to clarify:

  1. If variables are defined / declared outside any function body in a compilation unit, you must specify the storage class , e.g. static
  2. These variables will be put to BSS section of you executable file. And you'd better initialize then.
  3. When defining an array, you could use initializer {...} to initialize it. But you cannot use it in assignment statement.
  4. double rob_leftcolor[3] = {1.0, 0.0, 0.0} is a definition, while rob_leftcolor[3] = {1.0, 0.0, 0.0} is an assignment, so you can not use initializer here.
  5. Please make sure you know the differences between declaration and definition in C.

For storage class, consider the case below, without static, you are actually defining global variables:

// 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 
6

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like