Friday, 18 May 2012

What is callback function

There is a examples of callback function.


typedef struct
{
    int a;
    int b;
    int (* callback)(int a,int b);
}struct_b;


/*Assume that this is ur library i,e lower level function*/
int mylib(struct_b );


/*Assume that this ur higher level function*/
int add(int a,int b);


void main(void)
{
    int a,b;
    struct_b st_b;


/*Here u r passing higher level function to lower level function through structure*/
    st_b.callback = add;
    st_b.a = 10;
    st_b.b = 12;
    printf(“sum of a and b is = %d”,mylib(st_b));
}


/*This is ur higher level function definition*/
int add(int a,int b)
{
    return a+b;
}
int mylib(struct_b st_b)
{


/*Here u r calling higher level function with the help of call back function*/
    return st_b.callback(st_b.a,st_b.b);
}

No comments:

Post a Comment