medium10 pts · first answer countsembedded-cfunction-pointers
A command dispatch table
A UART protocol has command IDs 0–7. Why use a const array of function pointers indexed by command ID instead of a long switch?
typedef void (*cmd_fn)(const uint8_t *arg);
static const cmd_fn cmds[8] = { cmd_ping, cmd_read, cmd_write, /* ... */ };
if (id < 8 && cmds[id]) cmds[id](arg);Discussion 0
Join free to post in the discussion.
Related questions
- Why use volatileEmbedded Systems Engineer
- Setting a register bitEmbedded Systems Engineer
- Reading a hex register valueEmbedded Systems Engineer
- Adding to a uint8_t counterEmbedded Systems Engineer
- Where const goes in a pointerEmbedded Systems Engineer
- A static local counterEmbedded Systems Engineer
- Why a cross-compilerEmbedded Systems Engineer
- Keeping a table out of RAMEmbedded Systems Engineer