C program on Callback function (Function pointer)

 C program on Callback function (Function pointer)


“In computer programming, a callback is a reference to executable code, or a piece of executable code, that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.”


Function Pointers sample code :

#include<stdio.h>

int func(int, int);

int main(void)

{

    int result1,result2;

    /* declaring a pointer to a function which takes

       two int arguments and returns an integer as result */

    int (*ptrFunc)(int,int);


    /* assigning ptrFunc to func's address */                    

    ptrFunc=func;


    /* calling func() through explicit dereference */

    result1 = (*ptrFunc)(10,20);


    /* calling func() through implicit dereference */        

    result2 = ptrFunc(10,20);              

    printf("result1 = %d result2 = %d\n",result1,result2);

    return 0;

}


int func(int x, int y)

{

    return x+y;

}

 Registered Call back Function sample code

"The higher layer function calls a lower layer function as a normal call and the callback mechanism allows the lower layer function to call the higher layer function through a pointer to a callback function."



C program


/* callback.c */
#include<stdio.h>

/* Function Declaration Prototype  */
typedef void (*callback)(void);
void register_callback(callback ptr_reg_callback);

/* registration goes here */
void register_callback(callback ptr_reg_callback)
{
    printf("###############################################\n");
    printf("Inside register_callback\n");
    printf("In this example, the register_callback\n"
           "and callback function are in user space.\n"
           "In case, the callback function is a 'signal handler',\n"
           "then the 'signal handler' callback function is registered\n"
           "in kernel space, which runs in context of user space process.\n"
           "When the user space process receives the signal, the signal\n"
           "handler callback function from userspace is executed from kernel\n"
           "space. Hence, this is how call back is achieved.\n");  
    printf("###############################################\n\n");

    /* calling our callback function func */
    (*ptr_reg_callback)();
}


/* callback function definition goes here */
void func(void)
{
    printf("###############################################\n");
    printf("inside the invoked func, that is callback registered\n");
    printf("###############################################\n\n");
    return;
}

int main(void)
{
    printf("###############################################\n");
    printf("Main Program Starts\n");
    printf("This is a program demonstrating function callback\n");
    printf("###############################################\n\n");

    /* initialize function pointer to func */
    callback add_callback = func;

    /* register our callback function */
    register_callback(add_callback);

    printf("###############################################\n");
    printf("back inside main program\n");
    printf("###############################################\n\n");

    return 0;
}

Output:  ./a.out 

###############################################
Main Program Starts
This is a program demonstrating function callback
###############################################

###############################################
Inside register_callback
In this example, the register_callback
and callback function are in user space.
In case, the callback function is a 'signal handler',
then the 'signal handler' callback function is registered
in kernel space, which runs in context of user space process.
When the user space process receives the signal, the signal
handler callback function from userspace is executed from kernel
space. Hence, this is how call back is achieved.
###############################################

###############################################
inside the invoked func, that is callback registered
###############################################

###############################################
back inside main program
###############################################

< Just remember, callback is nothing but passing the function pointer to the code from where you want your handler/callback function to be invoked.  >