//Pointer to Function in C

 //Pointer to Function

As we know we can create pointer to variables in C language.We can also create pointer to functions in C.

Example:

#include<stdio.h>

void sum()//body of sum function

{

    int num1,num2,res;

    num1=10;

    num2=20;

    res=num1+num2;

    printf("\n The sum is %d",res);


}//end of sum

void subtract() //body of subtract function

{

    int num1,num2,res;

    num1=100;

    num2=20;

    res=num1-num2;

    printf("\n The subtraction is %d",res);

}//end of subtract function


int main()

{

    void (*fun_ptr)();//declaring pointer to function taking no                                    //arguments and also not returning any value

    fun_ptr=&sum; //assigning address of sum function

    printf("\n now calling sum function through pointer");

    fun_ptr();//calling sum function

    fun_ptr=&subtract;//assigning address of subtract function

    printf("\n now calling subtraction function through pointer");

    fun_ptr(); //calling subtract function

}//end of main function



Comments