Can array values be variables?

Hello,

I have a question about arrays.

Is it possible for the array's values to be variables?

For instance:

int a, b, c, d, e;

a = 1;
b = 2;
c = 3;
d = 4;
e = 5;

int myInts[5] = {a, b, c, d, e};

Is this a valid way to declare the array's values?

If so, is it possible to afterwards change those values, or will it be considered redefining (of "myInts" in my example).

//some function changes variables a, b, c, d and e value

a = 55;
b = 60;
c = 65;
d = 70;
e = 75;

int myInts[5] = {a, b, c, d, e};      //is this a redefinition?

If it is not a valid way to declare those values, any suggestions on how is it doable? should I use something different than Arrays, or are there some details that have to be added?

(two dimensional arrays wouldn't accomplish the task because I need the array's values to change very often; basically, every few milliseconds or so, because it will be storing data from an input and working with it right away. When done working, it will need to use new data -- it's a real-time program)

Thank you very much,
Mike

P.S. if there's something I didn't explain well, ask me to clarify it and I will do so without problem.

Hi,
You may find possible solution for your query.
http://php.net/array_combine
http://php.net/manual/en/function.extract.php

Kelvin-S:
Hi,
You may find possible solution for your query.
PHP: array_combine - Manual
PHP: extract - Manual

Thank you Kelvin! I will certainly take a look. :slight_smile:

Mike

int myInts[5] = {a, b, c, d, e}; //is this a redefinition?

Yes, if it is done in the same scope as the original declaration.

As to a solution, instead of doing

//some function changes variables a, b, c, d and e value

a = 55;
b = 60;
c = 65;
d = 70;
e = 75;

why not do

//some function changes variables

myInts[0] = 55;
myInts[1] = 60;
etc

why not do

//some function changes variables

myInts[0] = 55;
myInts[1] = 60;
etc

I see, thank you, but my goal is for the value to be set on its own, not manually in the code. What I mean, is something of the kind:

#define NumOfInt 5;

void loop ()
{
a = random(10); 

for(b = 0; b < NumOfInt; b++)
{
myInts[b] = a; 
}

// at the point the whole array is defined and we can 
work with its values and after that, set new values
into the array,  correct? 
}

Does this make sense?

for(b = 0; b < NumOfInt; b++)
{
myInts[b] = a; 
}

There is nothing wrong with that, so what is the problem ?

UKHeliBob:

for(b = 0; b < NumOfInt; b++)

{
myInts[b] = a;
}



There is nothing wrong with that, so what is the problem ?

If there's nothing wrong, then there's no problem!
Thank you very much , that's what I was willing to understand.
:slight_smile:

Take the idea further and do away with variable a etc
Save the value directly in the myInts[] array when you have calculated it/read it/whatever

You could do clever stuff with an array of references, but it would most likely be unnecessary

UKHeliBob:
Take the idea further and do away with variable a etc
Save the value directly in the myInts[] array when you have calculated it/read it/whatever

Thank you very much, I shall try this and see the result :slight_smile:

Groove:
You could do clever stuff with an array of references, but it would most likely be unnecessary

I guess that you're right, especially since I have only been coding on the arduino for less than 2 weeks.

But, so far this method is something that I can understand so I will try sticking to it for the moment even if unnecessary.

As for an array of references, could "myInts[5]= {a, b, c, etc};" be considered an array of references?

I've just read a bit about it, since I have never heard of it before, and it says that c++ doesn't support arrays of references too well...

Here's the link to what I've read:

Thank you in advance,
Mike

I confess I didn't know an array of references was impossible, just not very useful.

Groove:
I confess I didn't know an array of references was impossible, just not very useful.

No worries! Thank you very much anyway! :slight_smile: