8

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

Community
  • 1
  • 1
user2930701
  • 163
  • 1
  • 1
  • 5

4 Answers4

14

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
  • 6,994
  • 20
  • 29
  • 4
    Because that's how you init arrays in C. – Charlie Burns Nov 20 '13 at 03:50
  • @user2930701 because the compiler needs to know the size if the array size is empty... if it isn't then it is just because the c language says so. – Grady Player Nov 20 '13 at 03:51
  • 2
    Yes, using `{...}` as initializer for an array is *ONLY* for initialization, not for assignment. Storage class must be specified for these variables, as well, e.g. `static double rob_size = 1.0` otherwise your compile will warn you. – raof01 Nov 20 '13 at 03:51
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.

robertm.tum
  • 350
  • 2
  • 12
1

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.

DerStrom8
  • 1,311
  • 2
  • 23
  • 45
0

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
raof01
  • 76
  • 3
  • Could you expand on the first concept of specifying the storage class? – user2930701 Nov 20 '13 at 03:59
  • 1
    You do not have to specify a storage class. – Charlie Burns Nov 20 '13 at 04:01
  • A storage class defines the scope (visibility) and life time of variables and/or functions within a C Program. There are following storage classes which can be used in a C Program : `auto`, `register`, `static`, `extern`. Actually `auto` is the default one, and you don't need to explicitly specify a variable with `auto` storage class. You could refer to "The C Programming Language" for storage class. – raof01 Nov 20 '13 at 04:02
  • Without storage class specified, the compile will warn you. – raof01 Nov 20 '13 at 04:03
  • Interesting, my gcc and clang do not warn. – Charlie Burns Nov 20 '13 at 13:43
  • @Charile yes compilers might not warn you according to the version/options you use. But you're in the risk because you're using "global variables", like demonstrated in my answer. – raof01 Nov 21 '13 at 00:12