Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, January 14, 2016

Implementing callback functions in C

Implementing callback functions in C


I am a newbie to C. I am trying to implement callback function using function pointers.

I am getting an error

:test_callback.c:10: error: expected identifier or ?(? before ?void?  

when I try to compile the following program:

#include    void (*callback) (void);    void callback_proc ()  {    printf ("Inside callback function\n");  }    void register ((void (*callback) (void)))  {    printf ("Inside registration \n");    callback (); /* Calling an initial callback with function pointer */  }    int main ()  {    callback = callback_proc;/* Assigning function to the function pointer */    register (callback);/* Passing the function pointer */    return 0;  }  

What is this error?Can anyone help?

Answer by stacker for Implementing callback functions in C


I would recommend to use a typedef

#include    typedef void (*callback_t) (void);  callback_t callback;    void callback_proc(void)  {      printf ("Inside callback function\n");  }    void reg( callback_t _callback )  {      printf ("Inside registration \n");      _callback();  }    int main ()  {      callback = callback_proc;      reg(callback);        return 0;  }  

EDIT: removed the register issue

Answer by cjg for Implementing callback functions in C


You can't use 'register' as a function name as it's a C keyword.

Answer by interjay for Implementing callback functions in C


  1. register is a C keyword: Use another name for the function.

  2. You have extra parantheses around the callback parameter. It should be:

    void funcName(void (*callback) (void))  

Answer by Michael Burr for Implementing callback functions in C


2 problems:

  • you can't use the name register as it's a keyword (not used often anymore, but it's still there)
  • change the definition of the function from

    void wasRegister((void (*callback) (void)))  

    to:

    void wasRegister(void (*callback) (void))  

    (get rid of the parens around the parameter's declaration.

Also you might get a warning about callback_proc() not having a matching delaration to the callback variable (depending on how you compile the program - as C or C++), so you might want to change its declaration to:

void callback_proc (void)  

to make it explicit that it takes no parameters.

Answer by Tim Post for Implementing callback functions in C


Have a look at type safe callbacks from ccan. Its one thing to expose a typed function pointer for the world to use, its another to ensure sane casting.

Answer by James Morris for Implementing callback functions in C


#include    typedef void (*callback_func) (void);    static callback_func the_callback = 0;    void process (void)  {    printf ("Inside process function\n");  }    void callback_register (callback_func cb)  {    the_callback = cb;    printf ("Inside registration \n");  }    void callback(void)  {      the_callback();  }    int main (void)  {    callback_register(process); /* Passing the function pointer */    callback();    return 0;  }  

Declaring the_callback static would make more sense if this code was modularized and then you would be forced to call callback_register in order to set it, and callback in order to call it - the_callback would not be accessible outside of the implementation (.c) only the function declarations would be in the header (.h).

Answer by mohan for Implementing callback functions in C


very good example.Please make it event based.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.