1

This is my code

int numLeft[5];
void init() 
{
     numLeft = {5,4,3,3,2};
}

When I tried compiling this code, I got this error: "error: expected expression before '{' token. I know in java something like this could work

int[] numLeft;
void init() {
       numLeft = {5,4,3,3,2};
}

Is there something i am missing in my C code? A quick google search isn't helping.

committedandroider
  • 7,958
  • 14
  • 59
  • 121
  • 1
    [duplicated?](http://stackoverflow.com/questions/3137671/declaring-and-initializing-arrays-in-c) Use `memcpy`. – code monkey Nov 06 '14 at 07:42

2 Answers2

2
int numleft[5] = {5,4,3,3,2}

is the way to go. Here numleft is the variable that is referring to the array, but to access each, you've to refer to it as numleft[0], numleft[1], likewise.

GeeYes
  • 78
  • 5
1

int numLeft[5] = {5,4,3,3,2}; wiil do the job


if you need to initialize the global array each time, need to use memcpy(), as suggested in the other duplicate answers.

Sourav Ghosh
  • 130,437
  • 16
  • 177
  • 247