C program to demonstrate the use of function pointer

      A function pointer is a special type of pointer which stores the address of a function and it can be assigned to any function. So function correspoding to that function pointer gets executed whenever it is called like a function.

function pointer program in c



Source Code :

#include<stdio.h>
int(*fpointer)(int, int);  /*Define a pointer to a function */
int add(int, int);        /* Define a few  functions. */
int sub(int, int);
int main()
{
 	fpointer= add;       /*	Put the address	of ‘add’ in ‘fpointer’*/
  	printf("%d \n", fpointer(4, 5));  /*Execute ‘add’ and print results */
 	fpointer=sub;	                  /* Repeat for ‘sub’ */
 	printf("%d \n", fpointer(6, 2));
 	return	0;
}
 int add(int a,	int b)
 {
   return(a + b);
 }
 int sub(int a,	int b)
 {
   return(a - b);
 }


edit-code-image

Output:

9
4

More Insights:

1. More about pointer.

More Content:Explore More