Changing the values in an array to send to a function (neatly)

after initialising like this:

byte myArray[5] = {2, 4, 8, 3, 6};

Later I want to change all the values using something like:
myArray = {7,2,9,4,5};

But I can't find the syntax or an example anywhere. Maybe it is not possible?

it's not possible with just one language instruction. you need to iterate over each index.

one trick is to put the array in a struct as the compiler knows how to copy structs.

An alternative is that you can use memcpy()

byte myArray[] = {2, 4, 8, 3, 6};
byte newData[] = {7, 2, 9, 4, 5};
...
memcpy(myArray, newData, sizeof myArray); // copy the content of newData into myArray

obviously types and size should match !

You won't, because you can't do it. You could, however, copy a set of values into your array from another one

byte myArray[] = {2, 4, 8, 3, 6};
byte myOtherArray[] = {2, 4, 8, 3, 6};

void setup()
{
  Serial.begin(115200);
  memcpy(myArray, myOtherArray, sizeof(myArray));
  for (int x = 0; x < sizeof(myArray); x++)
  {
    Serial.println(myArray[x]);
  }
}

void loop()
{
}

OK thanks. I made the classic mistake of asking the wrong question though.

What I really want to do is pass a set of values into a function (filling and then sending an array was my attempted solution).

I want to do :

sendmap(8, 8, 8, 8, 8, 5);

and receive the values into an indexed structure eg

void sendmap(byte mapvals[])  {
....

do you want a function with variable number of parameters or do you want to pass an array (with possibly a variable number of elements?)

As Arduino is really C++, I suggest you should spend some serious time reading about C++ templates and C++ vectors. In that world, you can do what you wish.

This forum reallyis not geared to answering indepth C++ capabilities as examples given will be non-applicable to the majority of readers and will confuse many, IMO.

I you do go down the C++ deep rabbit hole, be prepared to do some serious study as you are going to get little forum assistance. Buy a good textbook on the subject you are on the advanced side of C++.

that's a fair point we keep forgetting as many beginners are on small AVRs and std::vector is not available

I want to always send 6 values.
And I want to send different values from different parts of the code.

And I'd like to set (and hence see) the values either on the line or the line before in the code.

I could define a new local variable for each occasion but that seems somehow unsatisfactory? but maybe it's OK


byte array13[] = {8, 8, 8, 8, 8, 5};
sendmap(array13);

void sendmap(byte mapvals[])  {

Sending arrays this way is OK. The only catch is that in the function you don't know how many items you have in mapvals. It's lost along the way.


byte array1[] = {1, 2, 3, 4, 5, 6};
byte array2[] = {6, 5, 4, 3, 2, 1};

void sendmap(const byte mapvals[])  {
  for (byte i = 0; i < 6; i++) Serial.println(mapvals[i]);
  Serial.println("-----");
}

void setup() {
  Serial.begin(115200); Serial.println();
  sendmap(array1);
  sendmap(array2);
}

void loop() {}

One way to solve this is to pass the length as a parameter


byte array1[] = {1, 2, 3, 4, 5, 6};
byte array2[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};

void sendmap(const byte mapvals[], byte n)  {
  for (byte i = 0; i < n; i++) Serial.println(mapvals[i]);
  Serial.println("-----");
}

void setup() {
  Serial.begin(115200); Serial.println();
  sendmap(array1, 6);
  sendmap(array2, 10);
}

void loop() {}
1 Like

I don't need it (today) because I know the length (=6).

But I would expect to be able to interrogate the length of the received array. Are you saying sizeof(mapvals) won't work? (I'm not quick enough to test it)

I notice you've put a const in there, what's that for ?.

it will work but will say 2 or 4, the size of a pointer. (in the function what you get is a pointer to the first element of the array)

look for " Array decaying" ➜ What is Array Decay in C++?

OK, I've tested it, and indeed sizeof(mapvals) returns 2 rather than 6.

So thanks for the solution(s). It's like you knew what I was going to try next :slight_smile:

this is to instruct the compiler that the function does not modify the array and so it's fine to accept const or non const parameters

if you try to compile this

const byte array1[] = {1, 2, 3, 4, 5, 6};
const byte array2[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};

void sendmap(byte mapvals[], byte n)  {
  for (byte i = 0; i < n; i++) Serial.println(mapvals[i]);
  Serial.println("-----");
}

void setup() {
  Serial.begin(115200); Serial.println();
  sendmap(array1, 6);
  sendmap(array2, 10);
}

void loop() {}

the compiler will complain and say

warning: invalid conversion from 'const byte* {aka const unsigned char*}' to 'byte* {aka unsigned char*}' [-fpermissive]
   sendmap(array1, 6);
                    ^

Thanks. I would send you some karma but that seems to have disappeared. Do I have to like all the helpful posts now?

:slight_smile:
As long as it helps - that’s good for me

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.