function pointers callbacks C
- by robUK
Hello,
I have started to review callbacks. I found this link:
http://stackoverflow.com/questions/142789/what-is-a-callback-in-c-and-how-are-they-implemented
which has a good example of callback which is very similar to what we use at work. However, I have tried to get it to work, but I have many errors.
#include <stdio.h>
/* Is the actual function pointer? */
typedef void (*event_cb_t)(const struct event *evt, void *user_data);
struct event_cb
{
event_cb_t cb;
void *data;
};
int event_cb_register(event_ct_t cb, void *user_data);
static void my_event_cb(const struct event *evt, void *data)
{
/* do some stuff */
}
int main(void)
{
event_cb_register(my_event_cb, &my_custom_data);
struct event_cb *callback;
callback->cb(event, callback->data);
return 0;
}
I know that callback use function pointers to store an address of a function.
But there is a few things that I find I don't understand. That is what is meet by "registering the callback" and "event dispatcher"?
Many thanks for any advice,