We may make use of typedef for declaring pointers to functions. Examine the following code:
typedef void(*PF) ();
Here, PF is the typedef for void functions with void arguments. Pointer to another function of this type may be declared as given below.
PF Function1;
According to this declaration, Function1 becomes pointer to functions of type void with void arguments. Similarly, typedef for an int function having two int arguments may be declared as given below.
typedef int(*PI) (int,int);
Now, PI may be used to declare functions of this type in the following manner:
PI Functions2 ;
According to this declaration, Function2 is a pointer to functions which return int and have two integer parameters.
Program Illustrates typedef for pointers to functions.
#include <stdio.h> void Funct1 () { printf("This is a void function with no arguments.\n"); } int Funct2(int a, int b) {return a*b ;} double Funct3 (double A, double B) {return (A*B);} int main() { int n = 20, m = 30; double x = 5.5, y = 4.0; typedef void(*PF) (); /*typedef of pointers to void functions*/ typedef int (*PI)(int,int); /*typedef of pointers to int functions with 2 int arguments*/ typedef double (*PD)(double, double); /*typedef of pointers to double functions with double arguments*/ PF Function1 = Funct1; /* Assigning the pointer with function name*/ PI Function2 = Funct2; PD Function3 = Funct3; Funct1(); // function call Function1() ; // function call clrscr(); /*The following two statements also perform function calls*/ printf("Function2(%d, %d) = %d\n", n, m, Function2(n,m)); printf("Function3 (%f, %f) = %f\n", x, y, Function3(x,y)); return 0; }