I have a a few callback functions. i need to register them as a list. And callback all the registered functions in a main after a certain task is done.

I am new to callback functions. Please shed some insight on this

xcallback(register_callbackfunctions);
ycallback(register_callbackfunctions);


register_callbackfunctions()//not too sure what goes in as the parameters here
{

//create the list of the functions


}

int main(){


 if( certain_task_is_done){

 //callback all the functions in the list that was registered

 }


 }

It's a very straightforward usage of function pointers. Here's a quikie example using a stack:

#include <stdio.h>
#include <stddef.h>

#define CALLBACK_MAX 10

typedef void (*callback_t)(void);

static callback_t callbacks[CALLBACK_MAX];
static size_t n = 0;

void register_callback(callback_t callback)
{
    if (n == CALLBACK_MAX)
        return;

    callbacks[n++] = callback;
}

void run_callbacks(void)
{
    while (--n < (size_t)-1)
        callbacks[n]();
}

void foo(void) { puts("foo!"); }
void bar(void) { puts("bar!"); }
void baz(void) { puts("baz!"); }

int main(void)
{
    register_callback(&foo);
    register_callback(&bar);
    register_callback(&baz);

    run_callbacks();

    return 0;
}

Thanks for the input. Really appreciate it :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.