Here is an example of callbacks in C.

Let's say you want to write some code that allows registering
callbacks to be called when some event occurs.

First define the type of function used for the callback:

typedef void (*event_cb_t)(const struct event *evt, void *userdata);
Now, define a function that is used to register a callback:

int event_cb_register(event_cb_t cb, void *userdata);
This is what code would look like that registers a callback:

static void my_event_cb(const struct event *evt, void *data)
{
    /* do stuff and things with the event */
}

...
   event_cb_register(my_event_cb, &my_custom_data);
...
In the internals of the event dispatcher, the callback may be stored
in a struct that looks something like this:

struct event_cb {
    event_cb_t cb;
    void *data;
};
This is what the code looks like that executes a callback.

struct event_cb *callback;

...

/* Get the event_cb that you want to execute */

callback->cb(event, callback->data);

source: 
http://stackoverflow.com/questions/142789/what-is-a-callback-in-c-and-how-are-they-implemented

On 5/29/12, Gene <gene.ress...@gmail.com> wrote:
> A callback is a function, say B, that you provide to some other
> function F in order to control F's behavior.
>
> The intuition is that F is defined with a "hole" in its specification
> that it fills up by "calling back" to the B you furnished.
>
> A simple example of a callback is the comparison function argument of
> qsort() .  For a more interesting example, look up the API for zlib,
> which is nicely designed with several callbacks.  The raw Win32 API
> also uses callbacks for various purposes.
>
> In the C language, callback is through a simple function pointer to B
> because that's all you have. In higher level languages, the function
> can carry data in a closure. With a Java-like object model, a
> "listener" object can be provided with both data and one or more
> methods. The normal way to approximate this higher level language
> behavior is by defining the C API with a void* parameter to F that it
> in turn passes back to B.  So when you call F, you can also provide
> data for B to use when F calls it later.  The zlib API uses this
> technique.
>
> On May 28, 4:22 pm, "rahul r. srivastava" <rahul.ranjan...@gmail.com>
> wrote:
>> whats a "CALL BACK" in C?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to