I'm trying to code an array of pointers of variables from a struct. My problem is that the variables inside the struct have different data type.
#include "stdlib.h"
typedef struct val {
unsigned char a;
unsigned char b;
unsigned short c;
unsigned int d;
} _letters;
void setup() {
Serial.begin(9600);
}
int var1 = 0;
void loop() {
_letters lt;
lt.a = 1;
lt.b = 2;
lt.c = 3;
lt.d = 4;
unsigned char *ptrLetters[4];
ptrLetters[0] = <.a;
ptrLetters[1] = <.b;
ptrLetters[2] = <.c; //here is the problem
ptrLetters[3] = <.d; //also here
var1 = (int)*ptrLetters[0];
Serial.println(var1);
}
The purpose of this is because I want to save the address and access the variables from the struct (which I CAN'T modify) by the index of the array (*ptrLetters[index]), but the problem is that inside the struct there are different data types and the pointer is initialised only for char types. How do I dynamically change that?
ptrLettersis too small for 4 elements.0..n-1when size isn. So for size3, indices are0..2So you need to have an array of 4 for indices0..3.