Populate Array in one shot after declaration?

I’ve googled around and not found an example of what I want to do, except in Java ... but what I want to do is declare an array, then populate it with a dataset later on after it’s declared... like this:

int arry[2];

void setup() {
//Do setup stuff
}

void loop() {
//do loop stuff
arry[] = {23, 45};
}

Apparently, in Java, you can do it like this:

int arry[2];

arry = new int[]{23, 45};

But this doesn’t work (obviously) in the Arduino.

Can this even be done?

And I should add, please don’t tell me about addressing each element like arry[0] = 21; etc. or tell me about looping and using the integer of the loop ... I’m fully aware of all that ... I need to do it in one line as I described in the OP.

int arry[2] = { 23, 45 };

or

int arry[] = { 23, 45 };

Regards,
Ray L.

RayLivingston:

int arry[2] = { 23, 45 };

or

int arry = { 23, 45 };




Regards,
Ray L.

OK, but this creates a NEW variable within the context of the subroutine and does not change the global variable declared at the beginning of the sketch. For example this sketch:

int arry[2];

void showArray() {
  Serial.println(String(arry[0]));
  Serial.println(String(arry[1]));
}

void setup() {
Serial.begin(9600);
}

void loop() {
int arry[2] = {23, 45};
showArray();
Serial.println(String(arry[0]));
Serial.println(String(arry[1]));
while (1==1){}
}

Provides this output:
0
0
23
45

Have a look at the memcpy() function. As long as the source data is itself in an array then it can be copied to another array

UKHeliBob:
Have a look at the memcpy() function. As long as the source data is itself in an array then it can be copied to another array

Thats interesting, although when I tested this using something like this:

int xy[3];
int xyz[3];

void proc() 

{

xy[0] = 23;
xy[1] = 24;
xy[2] = 25;

memcpy( xyz, xy, 2);

Serial.println(xy[0]);
Serial.println(xy[1]);
Serial.println(xy[2]);
Serial.println(“ “);
Serial.println(xyz[0]);
Serial.println(xyz[1]);
Serial.println(xyz[2]);

}

I get this output:

23
24
25

23
0
0

Not sure why it doesn’t copy all the elements in the array...

HOWEVER, using another function doesn’t really make sense in my case ... if I were going to do that, I would simply define a local array as a temporary storage then copy each element back into the global array. I was trying to avoid doing that for simplicity, but it doesn’t look like it’s possible with base C++.

Not sure why it doesn't copy all the elements in the array...

What is the third parameter in memcpy() ? How many entries do you have in your array ?

arry[] = {23, 45};

Yeah. You can't do that in C. I don't think you can do that in C++.

The memcpy() solution, used with various constant pgmspace arrays (and memcpy_P), is probably the closest you can get:

int myvariable[3];
static const int progmem varValues1 = { 1, 2, 3 };
static const int progmem varValues2 = { 10, 9, 8 };

void loop() {
    :
    memcpy_P(myvariable, varValues1, sizeof(myVariable));

(that's also probably about as efficient as you can get, even compared to a language that supports group constants natively. In the end, the cpu has the same sort of work to do, and the same instruction set to do it.)