Practice · 1 of 4
The Command Table
CHALLENGE
Difficulty: intermediate+25 XP
Implement a table-driven calculator:
``c
/* runs the named binary command: "add", "sub", "mul".
Returns the result, or INT_MAX for an unknown name. */
int calc(const char *name, int a, int b);
/* returns the number of registered commands */
size_t calc_count(void);
/* returns 1 if name is registered, 0 otherwise */
int calc_has(const char *name);
``
The dispatch table must be data (an array the functions walk) — no strcmp
if-chains inside calc.
Press Submit to check your solution.
Back to lesson: Practice: Dispatch & Context Gym