callbacks in c
-
Hi All, I am learning callbacks in c.I found an example in callback but could not understand .Can some one please explain me how the flow of execution happens and brief explaination of program. PFA code what I found.
* callback.c */
#include
#include"reg_callback.h"/* callback function definition goes here */
void my_callback(void)
{
printf("inside my_callback\n");
}int main(void)
{
/* initialize function pointer to
my_callback */
callback ptr_my_callback=my_callback;
printf("This is a program demonstrating function callback\n");
/* register our callback function */
register_callback(ptr_my_callback);
printf("back inside main program\n");
return 0;
}/* reg_callback.h */
typedef void (*callback)(void);
void register_callback(callback ptr_reg_callback);
/* reg_callback.c */
#include
#include"reg_callback.h"/* registration goes here */
void register_callback(callback ptr_reg_callback)
{
printf("inside register_callback\n");
/* calling our callback function my_callback */
(*ptr_reg_callback)();
} -
Hi All, I am learning callbacks in c.I found an example in callback but could not understand .Can some one please explain me how the flow of execution happens and brief explaination of program. PFA code what I found.
* callback.c */
#include
#include"reg_callback.h"/* callback function definition goes here */
void my_callback(void)
{
printf("inside my_callback\n");
}int main(void)
{
/* initialize function pointer to
my_callback */
callback ptr_my_callback=my_callback;
printf("This is a program demonstrating function callback\n");
/* register our callback function */
register_callback(ptr_my_callback);
printf("back inside main program\n");
return 0;
}/* reg_callback.h */
typedef void (*callback)(void);
void register_callback(callback ptr_reg_callback);
/* reg_callback.c */
#include
#include"reg_callback.h"/* registration goes here */
void register_callback(callback ptr_reg_callback)
{
printf("inside register_callback\n");
/* calling our callback function my_callback */
(*ptr_reg_callback)();
}void my_callback(void)... /* This is the callback function that will be invoked. */
int main(void)
{
callback ptr_my_callback = my_callback; /* ptr_my_callback points to the function my_callback */
register_callback(ptr_my_callback); /* registers the callback */
}/* This defines the type "callback". The '*' before "callback", which is in
parentheses, means that it's a pointer to a function. The function returns
void and takes (void) as its parameter. */typedef void (*callback)(void);
/* This function is called to register the callback. It actually doesn't do
that, because registering would mean saving a pointer to it. All that it does
is invoke the function immediately, which is what it could do later if it
saved its parameter somewhere and used it to call the function when it was
time to do so. To call the function, it dereferences the pointer to it and
invokes it with no arguments. */void register_callback(callback ptr_reg_callback)
{
(*ptr_reg_callback)();
}The
printf
s are just there so that you can observe the flow of execution.Robust Services Core | Software Techniques for Lemmings | Articles
The fox knows many things, but the hedgehog knows one big thing. -
Hi All, I am learning callbacks in c.I found an example in callback but could not understand .Can some one please explain me how the flow of execution happens and brief explaination of program. PFA code what I found.
* callback.c */
#include
#include"reg_callback.h"/* callback function definition goes here */
void my_callback(void)
{
printf("inside my_callback\n");
}int main(void)
{
/* initialize function pointer to
my_callback */
callback ptr_my_callback=my_callback;
printf("This is a program demonstrating function callback\n");
/* register our callback function */
register_callback(ptr_my_callback);
printf("back inside main program\n");
return 0;
}/* reg_callback.h */
typedef void (*callback)(void);
void register_callback(callback ptr_reg_callback);
/* reg_callback.c */
#include
#include"reg_callback.h"/* registration goes here */
void register_callback(callback ptr_reg_callback)
{
printf("inside register_callback\n");
/* calling our callback function my_callback */
(*ptr_reg_callback)();
}You might already be familiar with callbacks and not realize it. If you've ever used
qsort()
orbsearch()
, or another library function that takes a function pointer as an argument to help it do its work, you have already used callbacks! For example the signature for qsort isvoid qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *));
and might be used like this
struct foo {
char key[10];
int id;
/* more members */
};/* comparison function to sort struct foo by key */
int cmpfoo_bykey(const void *vp1, const void *vp2)
{
const struct foo *foo1 = vp1;
const struct foo *foo2 = vp2;return strcmp(foo1->key, foo2->key);
}/* comparison function to sort struct foo by id */
int cmpfoo_byid(const void *vp1, const void *vp2)
{
const struct foo *foo1 = vp1;
const struct foo *foo2 = vp2;return foo1->id - foo2->id;
}int main()
{
struct foo my_data[100];
/* load data into my_data : not show here*//* sort my_data by key field */
qsort(my_data, 100, sizeof(struct foo), cmpfoo_bykey);/* use my_data in key-ordered way ... */
/* sort my_data by id field */
qsort(my_data, 100, sizeof(struct foo), cmpfoo_byid);/* use my_data in id-ordered way ... */
return 0;
}In the above example
qsort()
knows how to sort an array of items in a generic way, but not how to compare items. It gets around this limitation by using a callback function, that the programmer provides, that does know how to compare items in the array. So, wheneverqsort()
needs to know how two items in the array compare to each other, it calls back to the code provided by the programmer to determine how the two items compare to each other.Keep Calm and Carry On