Skip to content
RED SAGSkills
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);
Choose one answer

Discussion 0

Join free to post in the discussion.

Related questions