Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 160663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:04:30+00:00 2026-05-11T11:04:30+00:00

I have started to review callbacks. I found this link on SO: What is

  • 0

I have started to review callbacks. I found this link on SO: What is a "callback" in C and how are they implemented? It 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 callbacks use function pointers to store an address of a function. But there are a few things that I find I don’t understand:

  • What is meant by ‘registering the callback’ and ‘event dispatcher’?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T11:04:31+00:00Added an answer on May 11, 2026 at 11:04 am

    This code compiles and runs under GCC with -Wall.

    #include <stdio.h>  struct event_cb;  typedef void (*event_cb_t)(const struct event_cb *evt, void *user_data);  struct event_cb {     event_cb_t cb;     void *data; };  static struct event_cb saved = { 0, 0 };  void event_cb_register(event_cb_t cb, void *user_data) {     saved.cb = cb;     saved.data = user_data; }  static void my_event_cb(const struct event_cb *evt, void *data) {     printf('in %s\n', __func__);     printf('data1: %s\n', (const char *)data);     printf('data2: %s\n', (const char *)evt->data); }  int main(void) {     char my_custom_data[40] = 'Hello!';     event_cb_register(my_event_cb, my_custom_data);      saved.cb(&saved, saved.data);      return 0; } 

    You probably need to review whether the call back function gets the whole struct event_cb or not – usually, you’d just pass the data because, as demonstrated, otherwise you have two sources of the same information (and a spare copy of the pointer to the function that you’re in). There is a lot of cleanup that can be done on this – but it does work.


    A question in the comments asks: Is this a good example of a callback?

    Succinctly, no – but in part because there isn’t sufficient infrastructure here.

    In a sense, you can think of the comparison function passed to the qsort() or bsearch() functions as a callback. It is a pointer to a function that is passed into the generic function that does what the generic function cannot do for itself.

    Another example of a callback is a signal handler function. You tell the system to call your function when the event – a signal – occurs. You set up the mechanisms ahead of time so that when the system needs to call a function, it knows which function to call.

    The example code is attempting to provide a more elaborate mechanism – a callback with a context. In C++, this would perhaps be a functor.

    Some of the code I work with has very fussy requirements about memory management – when used in a particular context. So, for testing, I use malloc() et al, but in production, I have to set the memory allocators to the specialized allocators. Then I provide a function call in the package so that the fussy code can override the default memory allocators with its own surrogate versions – and provided the surrogates work OK, the code will behave as before. Those are a form of callback – again, a form that does not need much (or anything) in the way of user context data.

    Windowing systems have event handlers (callbacks) that are registered and that the GUI main event loop will call when events occur. Those usually need user context as well as the event-specific information provided by the GUI system.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.