CKTIS / C
Callback dispatch tables in C
Map operation IDs to function pointers, call the selected callback indirectly, and distinguish an invalid index from a valid slot with no handler.
Version: ISO C23; implementation support may vary
Syntax
#include <stddef.h>
struct Process;
typedef void (*ProcessOp)(struct Process *, size_t);
enum { OP_ADD, OP_SHOW, OP_RESERVED, OP_COUNT };
static void add(struct Process *, size_t);
static void show(struct Process *, size_t);
static ProcessOp const ops[OP_COUNT] = {
[OP_ADD] = add,
[OP_SHOW] = &show,
[OP_RESERVED] = NULL,
};
/* Inside a function with parameters id, p, and n: */
/* if (id >= 0 && id < OP_COUNT && ops[id] != NULL)
ops[id](p, n); */A callback dispatch table maps operation IDs to function pointers. typedef void (*ProcessOp)(struct Process *, size_t); names the type of a pointer to a function that takes struct Process * and size_t and returns no value (void). If the Process tag is not yet visible, first declare struct Process; in the same scope as the typedef so the prototype refers to the intended structure type.
In the initializer, the function designator add converts implicitly to a pointer to that function; &show takes the address explicitly. Both functions must have types compatible with the function type that ProcessOp points to, including the return and parameter types. op(p, n) and (*op)(p, n) are equivalent indirect calls. A cast does not fix an incompatible type: calling through it has undefined behavior. ISO C does not guarantee that function pointers can be stored portably in void *.
Check the range of id before reading ops[id], especially if the ID comes from outside the program. Then compare the loaded pointer with NULL, the null pointer constant macro provided by <stddef.h>. A null slot means the operation is unsupported here; an out-of-range ID is invalid. Both an out-of-bounds read and a call through a null pointer have undefined behavior.
const prevents assignment to this table's entries, not changes to the object passed via p. The function pointer does not carry context: pass it separately and keep the object alive for as long as the callback uses it. This pattern is valid in ISO C23 and uses no C23-only syntax; C23 support varies by implementation.
Examples
#include <stddef.h>
#include <stdio.h>
struct Process {
size_t total;
};
typedef void (*ProcessOp)(struct Process *, size_t);
enum { OP_ADD, OP_SHOW, OP_RESERVED, OP_COUNT };
enum DispatchResult { DISPATCH_OK, DISPATCH_BAD_ID, DISPATCH_UNSUPPORTED };
static void add(struct Process *p, size_t n)
{
p->total += n;
}
static void show(struct Process *p, size_t n)
{
(void)n;
printf("total=%zu\n", p->total);
}
static ProcessOp const ops[OP_COUNT] = {
[OP_ADD] = add,
[OP_SHOW] = &show,
[OP_RESERVED] = NULL,
};
static enum DispatchResult dispatch(int id, struct Process *p, size_t n)
{
if (id < 0 || id >= OP_COUNT) {
return DISPATCH_BAD_ID;
}
ProcessOp op = ops[id];
if (op == NULL) {
return DISPATCH_UNSUPPORTED;
}
op(p, n);
return DISPATCH_OK;
}
int main(void)
{
struct Process p = { .total = 2 };
if (dispatch(OP_ADD, &p, 3) != DISPATCH_OK ||
dispatch(OP_SHOW, &p, 0) != DISPATCH_OK) {
return 1;
}
if (dispatch(OP_RESERVED, &p, 0) == DISPATCH_UNSUPPORTED) {
puts("unsupported operation");
}
if (dispatch(-1, &p, 0) == DISPATCH_BAD_ID) {
puts("invalid operation id");
}
return 0;
}
Result
total=5 unsupported operation invalid operation id
`OP_ADD` changes `total` from 2 to 5, and `OP_SHOW` prints the result. `OP_RESERVED` is in range but holds `NULL`; `-1` is rejected before the table is read.
Related lessons
- C · Lesson 85 · Advanced · Version: 3