C Program to Demonstrate Pointers to Functions

In this program, you will learn to use pointers to functions. Pointer as a function parameter is used to hold addresses of arguments passed during function call. This is also known as call by reference. It is possible to declare a pointer pointing to a function which can then be used as an argument in another function. A pointer to a function is declared as follows, type (*pointer-name)(parameter); Here is an example : int (*sum)(); //legal declaration of pointer to function int *sum(); //This is not a declaration of pointer to function A function pointer can point to a specific function when it is assigned the name of that function. int sum(int, int); int (*fp)(int, int); fp = sum; Here fp is a pointer to a function sum. Now sum can be called using function pointer s along with providing the required argument values. fp (10, 20);



Source Code
#include<stdio.h>
int sum(int x, int y)
{
    return x+y;
}
void main()
{
    int s;
    int(*fp)(int, int);
    fp = sum;
    s = fp(10,12);
    printf("Sum = %d", s);
    getch();
}
Output
Sum = 22