Callback function is hard to trace, but sometimes it is very useful. Especially when you are designing libraries. Callback function is like asking your user to gives you a function name, and you will call that function under certain condition.
For example, you write a callback timer. It allows you to specified the duration and what function to call, and the function will be callback accordingly. “Run myfunction() every 10 seconds for 5 times”
Or you can create a function directory, passing a list of function name and ask the library to callback accordingly. “Callback success() if success, callback fail() if failed.”
Lets look at a simple function pointer example
void cbfunc()
{
printf("called");
}
int main ()
{
/* function pointer */
void (*callback)(void);
/* point to your callback function */
callback=(void *)cbfunc;
/* perform callback */
callback();
return 0;
}
How to pass argument to callback function?
Observered that function pointer to implement callback takes in void *, which indicates that it can takes in any type of variable including structure. Therefore you can pass in multiple arguments by structure.
typedef struct _myst
{
int a;
char b[10];
}myst;
void cbfunc(myst *mt)
{
fprintf(stdout,"called %d %s.",mt->a,mt->b);
}
int main()
{
/* func pointer */
void (*callback)(void *);
//param
myst m;
m.a=10;
strcpy(m.b,"123");
/* point to callback function */
callback = (void*)cbfunc;
/* perform callback and pass in the param */
callback(&m);
return 0;
}
Thank you for the example, please make a change in the first snap shot of code.
void (*callback)(void *);
to
void (*callback)(void);
yup…i agree with vladimir…but we can also do it like this…
void cbfunc()
{
printf(“called”);
}
int main ()
{
/* function pointer */
void (*callback)();
/* point to your callback function */
callback = &cbfunc;
/* perform callback */
callback();
return 0;
}
Thanks Vladimir, corrected.
@cebu, it seems more clean
its an awesome description.. thankyou very much all
It ’s very useful , thank you everybody.
good simple example
its Good understanding call back function in simple way
if your function is ” cbfunc(myst *mt)”
then ur callback function should be “void (*callback)(myst *) ” NOT this “void (*callback)(void) “
i m fine with his code. he explained in a simple manner how to use callback funciton.
Hello All
i am getting following error while compiling it on linux
error: invalid conversion from `void*’ to `void (*)(myst*)’
can anybody plz suggest why it is coming
You’ve kept the whole thing really simple and the explanation is very neatly written. Keep the good work up.
ya i agree with nitin i.e
if your function is ” cbfunc(myst *mt)”
then ur callback function should be “void (*callback)(myst *) ” NOT this “void (*callback)(void) “
I am new to Callback, can you make sure your code fixed and compiled, then post it again please. I learn from example, however, your code does not compiled. Thanks!
How do you put this in a class? When I try to make the callback function pointer a member variable pointing to a function in another class, it says ‘void (MyClass::)()’ does not match ‘void(*)()’
Oh, I was forgetting the (void*) in the ‘(void*)blankfunc’. But when I have the (void*) it says “invalid use of member( did you forget the ‘&’?)
I have a doubt.
i can call the function “cbfunc(myst *mt)” directly.
then why i am assigning “cbfunc(myst *mt)” function to “callback” function pointer & then calling it.
its not the example of a call back function.it is an example of function pointer
Actually FP is the way to implement Callback. So the way to implement callback is the way in which you call the pointer variable (with/without argument) which is the refference to the function on which it is assigned..