Hello,
I am wondering if there is a convenient way to access multiple structs (of different size), as if they were part of a larger array?
So, let’s say firstly, that I declare an array of long variable’s as follows;
long arrayoflong[2] = {1234,5678}
And I do something simple, like calculating the size of each of the individual long variable's (obviously pointless as they're all the same size, but just an example).
both long variable's are represented by the indexNumber 0 and 1 respectively, and the example checks the size for both.
Example 1:
long arrayoflong[2] = {1234,5678};
byte indexNumber = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int Size = sizeof(arrayoflong[indexNumber]);
Serial.println(Size);
//goto next variable
if (indexNumber == 1) {
indexNumber = 0;
}
else {
indexNumber++;
}
delay(2000);
}
However, If I instead used 2 structs of differing size, the easiest way I know of to access the 2 would be using a switch case statement, as follows;
So this is a repeat of the example above, but I'm calculating the sizes of both my structs.
txdata_0 and txdata_1 are represented as indexNumber 0 and 1 respectively.
Example 2:
byte indexNumber = 0;
struct TxStruct_0 {
long distance;
long Time;
long Day;
char Name[6];
};
TxStruct_0 txData_0 = {1000, 2000, 3000, "hello"};
struct TxStruct_1 {
long pressure;
long humidity;
long flowRate;
long weight;
char Name[6];
};
TxStruct_1 txData_1 = {4000, 5000, 6000, 7000, "world"};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int Size;
switch (indexNumber) {
case 0:
Size = sizeof(txData_0);
break;
case 1:
Size = sizeof(txData_1);
break;
}
Serial.println(Size);
//go to next struct
if (indexNumber == 1) {
indexNumber = 0;
}
else {
indexNumber++;
}
delay(2000);
}
Is there any way I could have both of these structs (of differing size and variables) within some kind of unified package, where they can be accessed similar to example 1 above, which would allow me to omit the switch...case statement?
So I could perhaps have a function like this, which gets the size of txData_0 at indexNumber 0 and the size of txData_1 at indexNumber 1?
int Size = sizeof(arrayofStructs[indexNumber]);
The closest example I've managed to find is the accessing of multiple int arrays (of differing size) using an additional struct: Multi-dimensional arrays with different number of elements - #2 by sterretje
Could something similar be used for multiple structs? Or is this just not how these things work?
thanks a lot.