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
  • 8,711
  • 14
  • 71
  • 126
  • 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.

Neuron
  • 5,141
  • 5
  • 38
  • 59
GeeYes
  • 88
  • 6
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.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261