"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

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;

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

Need Your Help

What is the best way to sort controls inside of a flowlayout panel?

c# .net vb.net

I am adding custom controls to a FlowLayoutPanel. Each control has a date property. I would like to sort the controls in the flowlayoutpanel based on the date property. I can't presort the controls

SQL script, are my scripts accurate?

mysql sql oracle sqlplus oracle-sqldeveloper

Okay, so I received my graded exam that I took last week, that is on SQL using Oracle Plus. Two of the questions that I answered were marked wrong, but I feel strongly that I am correct, but he is