I have hard time to choose between arrays and structures
Witch one should be better way to do it
I have 16 relays and I need to save some data for each port , current value and address and some other information.
array:
char *grp_addr[]= ["1/1/1", "1/1/2", "1/1/3", ...]; // relay address
int phys_port[] = {2, 3, 4, 5, 6, A1, A2 ...};
int last_value[] = {0, 0, 0 ...};
I was using it for looping through grp_addr and if address matched on one of loop, I could use that array index to get it physical port and current value
for(int i = 0;i<16;i++){
if(grp_addr[i]==value){
phys_port[i]=TRUE;
}
}
but should I use structures? Like:
struct data_struct
{
int port;
char *addr;
char status;
};
and then setup loop
data fields[16];
fields[0].port = 2;
fields[0].addr = "1/1/10";
fields[0].status = 0;
fields[1].port = 3;
fields[1].addr = "1/1/11";
fields[1].status = 0;
then I could use them like arrays...
for(int i = 0;i<16;i++){
if(fields[0].addr=="1/1/x"){
fields[0].status=TRUE;
digitalWrite(fields[0].port, HIGH);
}
}
I think both of them works, but arrays looks better, but struckt is easier to setup