Home > Back-end >  Two groups of enumeration how tricky maps
Two groups of enumeration how tricky maps

Time:10-12

C language development sometimes encountered two constant need to form a mapping relationship, such as a serial port to send and receive orders, with no obvious relationship between migration:

Typedef enum
{
EN_UART2_CMD_QUERYINFO=0 x00,
EN_UART2_CMD_WRITESN,
EN_UART2_CMD_READSN,
EN_UART2_CMD_READVER,
EN_UART2_CMD_LAUNCH,
EN_UART2_CMD_LOCK,
EN_UART2_CMD_RESET,
EN_UART2_CMD_SETSAVE,
EN_UART2_CMD_CLEAN,
EN_UART2_CMD_TEST,
} UART2_CMD_T;

Typedef enum
{
Xa0 EN_UART2_CMD_RSP_QUERYINFO=0,
Xa3 EN_UART2_CMD_RSP_WRITESN=0,
Xa4 EN_UART2_CMD_RSP_READSN=0,
Xac EN_UART2_CMD_RSP_READVER=0,
Xb0 EN_UART2_CMD_RSP_LAUNH=0,
EN_UART2_CMD_RSP_LOCK xb1=0,
Xb4 EN_UART2_CMD_RSP_RESET=0,
Xb5 EN_UART2_CMD_RSP_SETSAVE=0,
Xb6 EN_UART2_CMD_RSP_CLEAN=0,
Xb7 EN_UART2_CMD_RSP_TEST=0,
} UART2_CMD_RESPONSE_T;

The most stupid method is to write a switch function, form the corresponding relationship, there are other good short-cut method excuse me?

CodePudding user response:

In view of the first enumeration is starting from scratch, consider using an array, the subscript is input, the output value is,

CodePudding user response:

If UART2_CMD_T has been the accumulative 1, the method is good, but the members of the enumeration values of dancing, such as EN_UART2_CMD_TEST=0 xf0, that this method would be a waste of memory,

CodePudding user response:

Support the way to the first floor, to create a second array of an enum type, the array subscript is just the first set of enumerated values

CodePudding user response:

This way the enumeration is a good way to solve continuous, jump enumeration is there a better way?

CodePudding user response:

Have a map?

CodePudding user response:

Two groups of enumeration based mapping table? Unfortunately this method

CodePudding user response:

In my opinion:
For example:
{NAME1=29, NAME3=78, NAME2=A3}
{name3=4 BC, name1=759, name2=37954}
Uppercase and lowercase mutual mapping


Here are six numerical you think do you want to save in the program?
If you want to, so here we need at least 6 memory space size
So:
Array1 [3]={NAME1=29, NAME2=A3, NAME3=78};
Array2 [4]={name1=759, name2=37954, name3=4 BC};
Mapping has natural set up here,

As for what name1 these and subscript mapping is very casual
As an enumeration
Enum
{
NAME1=0,
NAME2.
NAME3,
Name1=0,
.
} names;

Can also define
  • Related