With a dispatch table, an array of function pointers indexed by the command code. You define a common handler type (typedef void (*Handler)(void);), populate an array Handler handlers[] = { cmd_start, cmd_stop, ... };, and dispatch with handlers[code]() after bounds-checking code and null-checking the entry. The advantages over a switch: it's O(1) indexed call rather than a chain of comparisons (though compilers often turn dense switches into jump tables anyway); adding a command is adding a table row, not editing growing control flow; and the table is data you can build or modify at runtime, register handlers dynamically, swap behavior, or drive a state machine by indexing on current state. The tradeoffs: you lose the compiler's exhaustiveness checking, and an indirect call can't be inlined.
C Programming · Interview question
How would you replace a large switch statement that maps command codes to handlers?
A strong answer
What a weak answer sounds like
You know the answer. Do you know what gets you dinged?
Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.
From the lesson
Function Pointers & Callbacks
Store a function in a variable, pass behavior as an argument, and build dispatch tables: the mechanism behind qsort, HAL callbacks, and interrupt vector tables.