Arrays or structures?

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

An array of structs would be my choice

Hello
try this

struct data_struct {
  int port;
  char *addr;
  char status;
} fields[] {
  {2,"1/1/10",0},
  {3,"1/1/11",0},
};
3 Likes

Neat... I havn't seen that before ^^

It is defining the array as part of its declaration, which is something that I am sure you have done before

Actually, im quite sure I havn't, tho last time I did structures was 2005, I might remember wrong also
(and I was saving dynamically generated ascii roguelike map data, so it might be that I just ignored it back then)

Are you saying that you have never written a line of code like

byte ledPins[] = {10, 11, 12};

yes, ofcourse, but I haven't tough to do it right after structure, even that now I can see its just datatype, but it didn't come to my mind

That's not how strings are compared, you compare addresses of strings in your version.

Why do you use a string anyway? Three bytes inside the structure should do the trick.
If you want to have fancy comparisons or output,
that can be easily done with a member function of your structure.

Remember, the only difference between a class and a struct is the default accessibility
(public for struct and private for class).

Hello,
and for the intialization of the IO pins you can use "ranged-based loop". The compiler is doeing some work for you.

for (auto field:fields) pinMode(field.port,OUTPUT);

I think your

should be

for (auto field:fields) pinMode(field.port, OUTPUT);

you are right :+1:

yeah, that looks tidy

oh yeah, my bad. that address is transmited in 2 bytes(16bit), it comes in form of 4bit,4bit and 8bit. Its just that I had to print it on serial for easier view so yeah. so I was automatically thinking string comp :smiley:

(I was planning at work on caffee break, so its more like pseudo code) I hope to be able start actual coding at this evening.

I prefer

  for (auto& field : fields) {
    pinMode(field.port, OUTPUT);
  }

it allows easy addition of other initialization and to me, the loop is more obvious.
The reference inhibits the copy of the other version.

just one other aspect of static initialization and order of fields in a structure. not all fields need an initial value. they are set to zero by default. this can be helpful if there are many fields in a structure.

so the above could have been initialized as

  { 2, "1/1/10"},
  { 3, "1/1/11"},

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.