indirect addressing - possible with Arduino ?

Hi !

I do PLC programming, and we often use what is called "Indirect addressing"

here's an example: I have 4 variables, called:

Variable1 Variable2 Variable3 Variable4

I want to use another variable, called "CurrentVariable", whose value will be from 1 to 4, manipulated by some other code.

let's assume that "CurrentVariable" is equal to 3, and I want to display the content of the corresponding variable (Variable3)

how can I do that ?

e.g.: Serial.print(Variable[CurrentVariable]);

Tried it, and it does not work. Tried a few variant without any success.

anyone has a solution ?

I know I can use something like :

if (CurrentVariable ==1) { Serial.print(Variable1); } if (CurrentVariable ==2) { Serial.print(Variable2); } if (CurrentVariable ==3) { Serial.print(Variable3); etc...

it will work, but if I need to deal with 25 variables, it's going to be ugly !!

thanks guy .

Use an array, which is indexed by a counter in your loop or in a control statement, like a for loop.

Then you can taste each indexed array variable for a certain flavour, and act according to its taste with an if structure or a case structure.

use the learning button on the top line above the forum and follow the links to the references section and the individual programming elements, for correct syntax.

very good point...

Never thought about the array ! Was mind-lock about the indirect addressing side effect !

Thanks Anders53

C pointers are a perfect example of indirect addressing