In C, a function pointer stores the address of a function. An array of function pointers is a collection of these pointers that can hold multiple function addresses in an array and we can call any function using its array index. In this chapter, we will learn about −
- Function Pointers in C
- Array of Function Pointers
Function Pointer in C
A function pointer is a pointer that stores the address of a function. Instead of using the function’s name, we can use the pointer to call the function directly.
Syntax of Function Pointer
Following is the syntax to declare a function pointer −
return_type(*pointer_name)(parameter_list);
Here, return_type is the type of value the function returns, pointer_name is the name of the pointer, and (parameter_list) shows the types of values the function takes as input.
Example of Function Pointers
Here’s a example of a function pointer. We create an add function that takes two int arguments and returns an int. We store its address in func_ptr and call it with 3 and 5 as arguments using func_ptr(3, 5).
#include <stdio.h>intadd(int a,int b){return a + b;}intmain(){int(*func_ptr)(int,int)= add;// store address of addprintf("Result: %d\n",func_ptr(3,5));// call add using pointerreturn0;}
The output of this program is shown below −
Result: 8
Array of Function Pointers
An array of function pointers is an array where each element is a function pointer, and each pointer stores the address of a different function. Using this array, we can call any function by using the array index.
Syntax of Array of Function Pointers
Following is the syntax to declare an array of function pointers −
return_type(*array_name[size])(parameter_list);
Here, return_type is the type that all functions in the array return. array_name is the name of the array of function pointers. size is the number of function pointers in the array, and (parameter_list) is the type of arguments that all functions in the array must take.
Note that all functions in the same array must have the same return type and parameters.
Example: Array of Funciton pointers
In this example, we define three functions and create an array funcArr to hold their addresses. We assign the add function to funcArr[0], the sub function to funcArr[1], and the mult function to funcArr[2]. We can then call any function using its index, for example, funcArr[1]() calls the sub function.
#include <stdio.h>//first functionvoidadd(){printf("Add function\n");}//second functionvoidsub(){printf("Subtract function\n");}//third functionvoidmult(){printf("Multiply function\n");}intmain(){void(*funcArr[3])();// array of 3 function pointers
funcArr[0]= add;
funcArr[1]= sub;
funcArr[2]= mult;
funcArr[0]();// calls add
funcArr[1]();// calls sub
funcArr[2]();// calls multreturn0;}</pre>
Below is the output of each function, where each function prints its name.
Add function
Subtract function
Multiply function
Example: Array of Function Pointers with Parameters
In this example, we use an array of function pointers where each function takes two integers as parameters. Here's how we did it −
- We create three functions add, sub, and mult, each taking two integers as input and returning an integer.
- We declare an array funcArr of function pointers to hold their addresses.
- Each index of the array points to one function: funcArr[0] points to add, funcArr[1] points to sub, and funcArr[2] points to mult.
- Then, we call each function through the array by passing two integers as arguments. For example, funcArr[0](10, 5) calls the add function with inputs 10 and 5.
#include <stdio.h>intadd(int a,int b){return a + b;}intsub(int a,int b){return a - b;}intmult(int a,int b){return a * b;}intmain(){// array of 3 function pointers taking (int, int) and returning intint(*funcArr[3])(int,int)={ add, sub, mult };printf("Add: %d\n", funcArr[0](10,5));// calls addprintf("Subtract: %d\n", funcArr[1](10,5));// calls subprintf("Multiply: %d\n", funcArr[2](10,5));// calls multreturn0;}
The output below shows the results of addition, subtraction, and multiplication for the given inputs −
Add: 15 Subtract: 5 Multiply: 50
Conclusion
In this chapter, we learned about function pointers and arrays of function pointers. A function pointer stores the address of a function, while an array of function pointers keeps multiple function pointers in one place, and we can call a function using array[index](arguments).
Leave a Reply