give names to each variable on an array

Hi folks,

Curious how can i name elements on an array.

Suppose this:

I Have four variables to store

Voltage,
Amperage,
Capacity,
RPM,

So I create an array:

values[voltage, amperage, capacity, RPM]
Now how do i create a loop to load this values from (for example) an input port each array variable?

Use an enumeration.

You could also use a struct

Structs have some benefits in that each variable in the struct can be a different type, e.g. Voltage could be float, but you may want to store RPM as an int

You can have an array of structs if you want to store multiple lots of the same group of data.

Roger's use of structs is a good one. I tend to think of arrays of structs as arrays for adults. You can also use symbolic constants and also consts:

#define VOLTAGE       0
#define AMPERAGE      1
const int CAPACITANCE 2
const int RPM         3
// some statements...

int myArray[4];
int val;

val = myArray[VOLTAGE];
// more code...
val = myArray[CAPACITANCE];

The symbolic constants using the #defines are simply textual replacements, they do not create variables in the true sense of the word. The consts do create variables so if we ever get a symbolic debugger, they will have an advantage over symbolic constants.

A struct is definitely the way to go. An array makes all the values inside it set to a single type, int, byte, boolean . . . whereas in a struct, each variable can have its own type. You can even put a struct in an array, if you ever feel the need to.

econjack:
The symbolic constants using the #defines are simply textual replacements, they do not create variables in the true sense of the word. The consts do create variables so if we ever get a symbolic debugger, they will have an advantage over symbolic constants.

That is what david was pointing out with an enum.

enum{
 VOLTAGE,
 AMPERAGE,
 CAPACITANCE,
 RPM,
};

int myArray[4];
int val;

void setup() {
  val = myArray[ VOLTAGE ];
  val = myArray[ CAPACITANCE ];
}

Nice one guys :wink:

you can even use the enum in a for loop as enums are ints.

enum{
 VOLTAGE,
 AMPERAGE,
 CAPACITANCE,
 RPM,
};

int myArray[4] = { 1,2,3,4};

void setup() 
{
  Serial.begin(115200);
  Serial.println("Start ");
  
  for (int idx = VOLTAGE; idx <= RPM; idx++)
  {
    Serial.println(myArray[idx]);
  }
}

void loop() 
{
}

That said I advise to go for a struct as rogerClark mentioned, for reasons already mentioned

With 2 arrays of equal size, I've used one array for data, another for the data labels. The data labels are strings that get stored in SDRAM and do not use up valuable ram (I think).

 char* data_label[]={"00: ", "01: ", "02: Hz L: ", "03: Hz H: ", "04: VoltsA Divider L: ", "05:  VoltsA Divider H: ", "06: VoltsB Divider L: ",
 "07: VoltsB Divider H: ", "08: VoltsC Divider L: ", "09: VoltsC Divider H: ", "0A: PS VoltsA Gain L: ","0B: VoltsA Gain H: ", "0C: VoltsB Gain L: ",
 "0D: VoltsB Gain H: ", "0E: VoltsC Gain L: ", "0F: VoltsC Gain H: ", "10: PhaseB Degrees L: ", "11: PhaseB Degrees H: ", "12: PhaseC Degrees L: ",
 "13: PhaseC Degrees H: ", "14: ", "15: ", "16: "};

EDIT: Re next reply:

Each string will need to be placed into PROGMEM, then an array of those pointers can be formed.
Otherwise the whole lot is in precious RAM.

Oops ... I meant to reply with "const char* data_label...", but access would be by index.
Great suggestions here ... I'll be testing the use of enumeration in my future coding.

dlloyd:
With 2 arrays of equal size, I've used one array for data, another for the data labels. The data labels are strings that get stored in SDRAM and do not use up valuable ram (I think).

Each string will need to be placed into PROGMEM, then an array of those pointers can be formed.
Otherwise the whole lot is in precious RAM.

So one last question.

Asusme i dont want to name the array variables.

I start by creating an array

int myInts[6];

How do I access the contents of cell 4, at any time, for example?

Also, is there an advantage to using an array if you only need small values of data such as 5 variables?
I know the advantage is huge when having a 10x10 array, but say for a 1 column and 6 rows?

Regards

int myInts[6];

How do I access the contents of cell 4, at any time, for example?

myInts will have 6 elements, indexed from 0 to 5. So the 4th element is index number 3, and would be accessed like this:

Serial.println(myInts[3]);
// or ...
byte arrayIndex = 3;
Serial.println(myInts[arrayIndex]);

The advantage of using arrays is that the index can be a variable and therefore it is easy to access all elements sequentially in a for loop, or access a specific element based on the result of a calculation. Otherwise, you would need lots of repeated code or complex if or switch statements.

Giving names is what #define does:

int array [N] ;

#define VOLTS array[0]
#define AMPS  array[1]

..
  VOLTS = AMPS * 50 ;

Also, is there an advantage to using an array if you only need small values of data such as 5 variables?
I know the advantage is huge when having a 10x10 array, but say for a 1 column and 6 rows?

Choice of data structure involves more than just the number of variables. One consideration is the algorithms that will be used. You should not put unrelated variables in an array just because there are many of them. With experience you will get a better feel for what data structure to use in a given situation.

This is a bit compiler dependant, but the code will probably be larger and run slower using arrays than individual variables.