C program to show function pointers as a parameter

C Programming Language / Pointer in C Language

1002

Program:

// A simple C program to show function pointers as parameter
#include <stdio.h>
 
// Two simple functions
void fun1() { printf("Fun1\n"); }
void fun2() { printf("Fun2\n"); }
 
// A function that receives a simple function
// as parameter and calls the function
void wrapper(void (*fun)())
{
    fun();
}
 
int main()
{
    wrapper(fun1);
    wrapper(fun2);
    return 0;
}

Output:

Fun1
Fun2

Explanation:

Like normal data pointers, a function pointer can be passed as an argument and can also be returned from a function.For example, consider this C program where wrapper() receives a void fun() as parameter and calls the passed function.


This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.