Codiqo

Learn C · lesson 60 of 60 · about 11 minutes

Project: Command Menu

Build a tiny command interpreter that looks up commands in a table of function pointers.

What you'll learn

  • Pair names with functions in an array of structs
  • Look up and run a command by name
  • Use an enum for the program's state

The on-call console

The report ends with a console for the on-call engineer. Commands are typed one per line until quit: raise disk and clear cpu change the night's alerts, and show lists them: Alerts: disk net (code 6). An alert name that isn't one gets That needs an alert: cpu, disk or net, and a command that isn't one gets Unknown command: reboot. When the console closes, the report says what's still open: Left open: disk net (code 6). The Command struct and the State enum are written for you.

The plan

The program reads commands like add 5 or show and changes a running total. Instead of a long chain of if-else, each command is a row in a table:

1. A struct pairs a command's name with the function that runs it
2. The program reads a word, finds the matching row with strcmp, and calls its function
3. An enum tracks whether the program should keep going or stop

The command table

#include <stdio.h>
#include <string.h>

typedef struct {
    const char *name;
    void (*run)(int *total);
} Command;

void inc(int *total) { (*total)++; }
void dbl(int *total) { *total *= 2; }
void show(int *total) { printf("%d\n", *total); }

Command commands[] = {{"inc", inc}, {"dbl", dbl}, {"show", show}};

int main(void) {
    int total = 1;
    const char *script[] = {"inc", "dbl", "show"};
    for (int i = 0; i < 3; i++) {
        for (int c = 0; c < 3; c++) {
            if (strcmp(script[i], commands[c].name) == 0) commands[c].run(&total);
        }
    }
    return 0;
}

Output

4

Adding a command

To add a new command you write one small function and add one row to the table; the lookup loop never changes. A RUNNING/STOPPED enum lets a quit command end the main loop cleanly.

Stopping with an enum

#include <stdio.h>

typedef enum { RUNNING, STOPPED } State;

int main(void) {
    State state = RUNNING;
    int steps = 0;
    while (state == RUNNING) {
        steps++;
        if (steps == 3) state = STOPPED;
    }
    printf("stopped after %d steps\n", steps);
    return 0;
}

Output

stopped after 3 steps

Clearing an alert

cmd_clear turns one bit off with *alerts &= ~flag: ~flag has every bit on except that one, so the & keeps everything else as it was. Each command function takes the alerts by pointer, so it changes the real ones in main, and gives back false when it needed an alert name and didn't get one. The loop finds the row whose name matches with strcmp and calls its function through console[i].run.

Tip

sizeof commands / sizeof commands[0] gives the number of rows, so the lookup loop keeps working when you add commands.

Now try it yourself

This lesson is free in Codiqo, with 9 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.