Function Pointers — syntax, callbacks, dispatch tables - MarekBykowski/readme GitHub Wiki

// Declaration: pointer to function taking int, returning int
int (*fp)(int);

// Assign and call
int square(int x) { return x * x; }
fp = square;
int result = fp(5);    // → 25
int result = (*fp)(5); // same — both are valid

// typedef makes it readable
typedef int (*transform_fn)(int);
transform_fn fn = square;

// As function parameter (callback)
void apply(int *arr, int n, transform_fn fn) {
    for (int i = 0; i < n; i++)
        arr[i] = fn(arr[i]);
}

// Dispatch table — replaces switch/if-else chains
typedef void (*cmd_handler)(void);
cmd_handler dispatch[] = {
    [CMD_ARM]    = handle_arm,
    [CMD_DISARM] = handle_disarm,
    [CMD_TAKEOFF]= handle_takeoff,
};
dispatch[cmd]();  // O(1) dispatch, no branching

💡 Dispatch tables are the embedded alternative to virtual functions in C++. Used heavily in drone firmware for command handling.