Why function pointer is used in C language?

C Linux code for : function pointer and callback function.
What is function Pointer?
->funtion pointer is pointer that holds the address of a function.
Why function pointers are used in C?
->function pointers are mainly used to implement callback functions.
What is callback function in C?
->callback functions are functions which are passed as argument to other functions and this passed callback function is invoked on some event.

->best example is sigaction system call. When given event/signal like alarm,SIGINT occurs,the function pointed by the pointer  is called through the function pointer in sigaction structure.example B

->another use of function pointer is generality like templates in c++.ie. with the same function pointer different
functions can be called.example A below.

/*Example A:
 same function pointer is used to call add and sub function*/
#include<stdio.h>
/*prototype here: funcPtr is function pointer taking two integers as arguments and returning an int
int (*funcPtr(int, int))
*/
/*prototype of registration function generally used to pass function pointer as argument*/
int regfunc(int (*funcPtr)(int, int), int,  int);
/*proto types of function called by function pointers*/
int add(int a, int b);
int sub(int a, int b);

/*definition of first function called through function pointer*/
int add(int a, int b)
{
        return a+b;
}
/*definition of second function called through function pointer*/
int sub(int a, int b)
{
        return b-a;
}
/*main for calling functions through function pointer */
int main()
{
        int a,b;
        a=regfunc(add,10,20);
        printf("sum is %d\n",a);

        b=regfunc(sub,10,20);
        printf("sub is %d\n",b);

        return 0;
}
/*definition of registration function that receives a funcPtr and two inta*/
int regfunc(int (*funcPtr)(int x,int y),  int x,  int y)
{
        return (*funcPtr)(x,y);
}
/*
sum is 30
sub is 10
*/

/*Example B:
->best example is sigaction system call. When given event/signal like alarm,SIGINT occurs,
the function pointed by the pointer act is called though the function pointer in sigaction structure.example B
*/
/*
struct sigaction
{
void (*sa_handler)()//address of sighandler or SIG_IGN or SIG_DEF
sigset_t sa_mask;//to block other signals
int sa_flags;
};
*/


/*
function pointer are reference to code that are passed as argument to different code. callback functions are function which are invoked on some event.
*/
#include<stdio.h>
#include<signal.h>
#include<unistd.h>


struct sigaction act;
/*call back function to be called by funcptr on alarm event*/
void sig_handler(int signo, siginfo_t *si, void *ucontext)
{
printf("Alarm signal received is:%d\n",signo);
}

int main()
{
/*intializig function pointer with func1*/
act.sa_handler=sig_handler;
act.sa_flags=SA_SIGINFO;
/*registering functon pointer to register function*/
/*ie passing functon pointer as argument to register function*/

/*Alarm event will cause call back function call through function pointer pointed by ptr act */
sigaction(SIGALRM,&act,NULL);

alarm(3);/*Now Alarm event will cause call back function call through function pointer pointed by ptr act */

/*waiting for any signal from kernel*/
pause();

/*after signal handler call back function executed*/
printf("in main again\n");
        return 0;
}

/*
Alarm signal received is:14
in main again
*/


/*
function pointer are reference to code that are passed as argument to different code. callback functions are function which are invoked on some event.
*/
#include<stdio.h>

typedef void (*funcPtr)(void);
void regfuncPtr(funcPtr myfp);
void func1();

int main()
{
/*intializig function pointer with func1*/
        funcPtr myfp=func1;
/*registering functon pointer to register function*/
/*ie passing functon pointer as argument to register function*/
        regfuncPtr(myfp);

        return 0;
}
/*defintion of register function*/
void regfuncPtr(funcPtr myfp)
{
        printf("inside regFuncPtr\n");
        (*myfp)();
}
/*definition of function to be called by function pointer*/
void func1()
{
        printf("inside func1\n");
}
/*
inside regFuncPtr
inside func1

*/

/*What is function pointer in C?
functon pointer are reference to code that are passed as argument to different code.
What is callback function in C?
callback functions are function which are invoked on some event.
*/
#include<stdio.h>
#include<signal.h>
#include<unistd.h>


/*Another example of call back function to be called by funcptr on alarm event in C language*/
void mysignalHandler(int signo)
{
printf("\n mysignalHAndler: Alarm signal received is:%d\n",signo);
}

int main()
{
signal(SIGINT,mysignalHandler);
/*create some delay*/
printf("sleeping for 10 seconds.\n");
printf("generate SIGINT by ctrl+c command: else normal exit\n");
sleep(10);

/*after signal handler call back function executed*/
printf("in main again\n");
        return 0;
}

/*
Alarm signal received is:14
in main again
*/
This page explained syntax and c linux code for :
pointer to function
function pointer
callback functions


1 comment:

  1. function pointer is also used as replacement of if else/switch statement.
    function pointer is used to implement callback function
    function pointer is used pass a function as parameter to function
    function pointer is used for generic purpose same function pointer is used to assigned with different functions.

    ReplyDelete