Arrays manipulation: making attribution to an array as a whole at once.

Hello all

I’ve got stuck on something which might have a very simple solution. I’d like to ask your help.

I’ve initialized an array as follows:

uint8_t linha[4] = {0,0,0,0};

The idea is to read its terms sequentially inside a loop, as a way to give a specific set of data for each phase in the program. The loop is ok, working fine. No trouble for reading every term one at once.

I’ve been finding hard time to attribute values to this array. I want to attribute all at once, just like a set of values according to each phase. Nevertheless, I haven’t found the right syntax to make it happen.

1st example:

case 5: linha = { 24, 5, 6, 27 }; break;

returns error: expected primary-expression before ‘]’ token

2nd example:

case 5: linha[4] = { 24, 5, 6, 27 }; break;

returns error: cannot convert ‘’ to ‘uint8_t {aka unsigned char}’ in assignment

The only one to make it work has been:

case 5: linha[0]=24; linha[1]= 5; linha[2]= 6; linha[3]=27; break;

It doesn’t sound very efficient. There must be any better way. Let’s think about an array with 25 parameters. How many lines it would take, how much time to type it all? It would be at least confusing to read latter.

Any better idea?

Thank you.
Regards, Ciro.

Are suggesting everyone should get their own custom compiler syntax?

Paul

I think you could have something like

case 5: memcpy(linha, { 24, 5, 6, 27 }, sizeof(linha[]));

My C is a little rusty so I'm not 100% on this.

Is this just variable initialization? If so use this.

uint8_t linha[4] = {24,5,6,27};

This wont work if the variables have already been initialized. The compiler is what initializes them. Once initialized, the only time they can change is if the Arduino does it and that's going to take multiple clock cycles.

aarg:
I think you could have something like

case 5: memcpy(linha, { 24, 5, 6, 27 }, sizeof(linha[]));

My C is a little rusty so I’m not 100% on this.

Hi Shannon Member

memcpy looks like a good solution. I haven’t found this function at Arduino Reference, but as it belongs to C++, it should exist by Arduino environment as well. And it does!

But it hasn’t worked yet. I guess two variables are required (source and target). Maybe I’ve got to create constant arrays as sources.

error: cannot convert ‘’ to ‘const void*’ for argument ‘2’ to ‘void* memcpy(void*, const void*, size_t)’

What do you understand from this error message?

Thank you.
Regards, Ciro.

cirobruno:
I guess two variables are required (source and target).

Look again.

memcpy() is a crude solution for this. Really, there is probably no need to do what you're asking. The C philosophy is to use pointers for aggregate data such as arrays, and copy pointers instead of members. It's not good design to have anonymous data like { 24, 5, 6, 27 } in a program. Also I was wrong about using an initalizer list that way. You can do:

char foo[] = { 24, 5, 6, 27 };
...
case 5: memcpy(linha, foo, sizeof(linha[]));

but as I suggested you would normally do something like:

char foo[] = { 24, 5, 6, 27 };
char *linha;
...
case 5: linha = foo;

What this does, is change the reference 'linha' to point to the contents of 'foo'. After executing the assignment, linha[1] and foo[1] will both return the value 5.