Filling an array on one line {a,b,c,d}

Hi, I have some code where I declare an array before the setup function

int array[] = {0,0,0,0,0,0,0,0};

I want to be able to fill it up with value just like as when it is declared but later in a a function. For example;

void Function() {
    array[] = {1,2,3,4,5,6,7,8};
}

But this gives me an error "expected primary-expression before']' token". How can I avoid this error while not having to resort to filling the array line by line?

RichMo:
Hi, I have some code where I declare an array before the setup function

int array[] = {0,0,0,0,0,0,0,0};

I want to be able to fill it up with value just like as when it is declared but later in a a function. For example;

void Function() {

array[] = {1,2,3,4,5,6,7,8};
}



But this gives me an error "expected primary-expression before']' token". How can I avoid this error while not having to resort to filling the array line by line?

or if you are trying to put values into the array you created with a global scope :

array[0] = 1;
array[1] = 2;
array[2] = 3;
... and so on
1 Like

There's no way to do it on one line? I'm trying to avoid doing it line by line like that, simply for readability in the code. I will be defining LED patterns in the array values so would ideally like them all on one line.

int sizeOfArray = 5;
int test[] = {1,2,3,4,5};

void setup(){
 // one line implementation of loading the array
  for (int i=0;i<sizeOfArray;i++) {test[i] = digitalRead(i);  }
};

Or something like that. I think where you're getting confused is in the construction of the array and its initialization.

It's a convenience afforded by the compiler that you can do both at the same time:

int array[] = [1,2,3,4,5];

rather than:

int array[5]; // allocate memory for 5 ints
array[0] = 1;
array[1] = 2;
...
array[4] = 5;

Really what's happening is that you're telling the compiler to initialize an array with 5 elements and 'oh by the way' here are the 5 things I want to start with.

Later when you use 'array' what you really have is an address pointing to the beginning of the array. The code and compiler at that point have no idea how long 'array' is. Could be 5 elements long, could be 500. To see what I mean try this code:

int sizeOfArray = 8; // I've intentionally set this value higher than what is allocated to test below.
int test[] = {1,2,3,4,5};

void setup(){
  Serial.begin(57600);
  for (int i=0;i<sizeOfArray;i++) {
    Serial.print(test[i],DEC);
    Serial.print(",");
  }
};

void loop(){

}

You'll end up with something like "1,2,3,4,5,684,328,206" where the last three 'entries' of the array are some garbage. The compiler doesn't know that the array is only 5 'int' elements long, and will happily read the next three memory locations in 'int' sized increments and spit them out at you.

This is where memory leaks and other nasty things come from. C is a very powerful language, but it's also very sparse and doesn't 'protect' you like some other higher level languages you might have used before do.

This is related to why you can't use the same 'initialization' method later on to just file the array as you'd like to.

1 Like

you optionally could use another array or declare it locally:

void Function() {
    int array[] = {1,2,3,4,5,6,7,8};
   // your code uses this array within the scope of loop()
}

or fill your array with another array

int myArray[8] ;
const int fullArray[8] = {1,2,3,4,5,6,7,8};

void setup()
{
  for (int i = 0; i < 8; i++)
  {
    myArray[i] = fullArray[i];
  }
}

void loop()
{
  
}

hope that helps.

If you want to fill an array of bytes with all the same value, you can use the C library function memset(). It puts the same byte value in all the bytes of your array.

If you want a sequence of numbers like your example, though, then no. You need to use a loop, or as somebody else pointed out, set up "master" arrays that contain the values you want and then copy them into your target array. For that you could use the C library function memcpy(), which will copy a block of bytes from 1 address to another:

static int up[] = {1, 2, 3, 4, 5};
static int down[] = {5, 4, 3, 2, 1};
static int upAndDown[] = {1, 2, 3, 2, 1};


int array[5];

memcpy() takes 3 parameters: The destination, the source, and a byte count. (Destination first, which is counterintuitive.)

memcpy(array, down, sizeof(down);  //array now contains {5, 4, 3, 2, 1}
memcpy(array, up, sizeof(down);  //array now contains {1, 2, 3, 4, 5}

RichMo:
Hi, I have some code where I declare an array before the setup function

int array[] = {0,0,0,0,0,0,0,0};

I want to be able to fill it up with value just like as when it is declared but later in a a function. For example;

void Function() {

array[] = {1,2,3,4,5,6,7,8};
}



But this gives me an error "expected primary-expression before']' token". How can I avoid this error while not having to resort to filling the array line by line?