C/C++ Arena

Step 6 of 6

Challenge: an event bus

A bare function pointer has no memory of its own. To let a callback update your data, C APIs pass a void *ctx ("context" or "user data") alongside it, and hand it back on every call:

typedef void (*Handler)(int event, void *ctx);

That's how GUI toolkits, pthread_create, network libraries and game engines all do callbacks in C.

Your turn: build a tiny event bus that stores up to 8 handler/context pairs.

Previous: Generic code with void *