Skip to main content

Checkpoint: The Callback Framework

Challenge on lesson: Checkpoint: Function Pointers

Build a small event framework on function pointers: ``c typedef struct { int id; int (*handler)(int); } Handler; /* in boilerplate */ /* registers handler under an id (id 0..63). Returns 1, or 0 if the id is taken or out of range or handler is NULL. */ int bus_register(int id, int (*handler)(int)); /* runs the handler registered under id on value v; returns its result, or -1 if no handler is registered there. */ int bus_emit(int id, int v); /* removes a handler; returns 1 if one was removed, 0 otherwise */ int bus_unregister(int id); /* runs ALL registered handlers on v, summing results; returns the sum */ int bus_broadcast(int v); ``

Difficulty: intermediate

Back to lesson: Checkpoint: Function Pointers