How to check the value of structure data in a switch statement

My code is:

#define qty     2
typedef struct { uint8_t ID;
                 uint8_t tagbyte[5];  
               } cards;

const cards eecollcards[qty] EEMEM ={
                                      {0x01, {0x2A, 0x00, 0x24, 0x91, 0xFD}},
                                      {0x02, {0x4F, 0x00, 0x88, 0x59, 0xB0}},
                                    };
int main (void)
while (1)
{
  switch (<what goes here?>)
  {
    case 0x01: .....
      break;
    case 0x02: .....
      break;
  }
}

I want to check the values ​​stored in structby distinguishing between the values ​​0x01 and 0x02, but I am having problems with the formation of the operator switchshown above. I tried cards.uint8_t IDand eecollcards[qty] EEMEM, but they generate errors such as:

error: excepted expression before struct...

I know that cardsthis is just a type name, not a variable. 0x01 refers to a IDtype variable uint8_t, and the remaining hexadecimal values ​​are for initializing the array tagbyte[5].

+4
source share
2 answers
int main()
{
  int i;
  const cards eecollcards[qty] ={
                                {0x01, {0x2A, 0x00, 0x24, 0x91, 0xFD}},
                                {0x02, {0x4F, 0x00, 0x88, 0x59, 0xB0}},
                                };
  for (i = 0; i < qty; i++)
  {
    switch(eecollcards[i].ID)
    {
      case 0x01: //.....
        break;
      case 0x02: //.....
        break;
    }
  }
  return 0;
}

I deleted EEMEM, otherwise it will not compile on my system

+3

EEMEM , (, EEPROM...). , ( ):

int i = 0;
while (i < qty) {
  switch (eecollcards[i].ID) {
  ...
  }
  i++;
}
+1

Source: https://habr.com/ru/post/1544715/


All Articles