C Pointer to a function

In this section, you will learn how to use 'Pointer'
function in C.
C provides a special feature of pointer to a
function. As you know that every function defined in C language have a
base address attached to it. This base address acts as an entering point into
that function. This address can be stored in a pointer known as function
pointer. The pointer to a function is the declaration of pointer that
holds the base address of the function. The declaration of a pointer to a
function is:
Syntax :
return_type (* pointer_name) ( variable1_type variable1_name ,
variable2_type variable2_name , variable3_type variable3_name
.................);
You can see in the given example, we have create a
function mul to find the product of three numbers. Then we have declared
the function pointer for storing the base address of function mul
in the following way:
int (*function_pointer) (int,int,int);
Here is the code:
POINTERF.C
#include <stdio.h>
#include <conio.h>
int mul(int a, int b, int c) {
return a*b*c;
}
void main() {
int (*function_pointer)(int, int, int);
function_pointer = mul;
printf("The product of three numbers is:%d", function_pointer(2, 3, 4));
getch();
}
|
Output will be displayed as:

Download the code

|