C pointer to array of function
#include<stdio.h> int display(); int(*arr[3])(); int(*(*ptr)[3])(); int main() { arr[0]=display; arr[1]=getch; ptr=&arr; printf("%d",(**ptr)()); (*(*ptr+1))(); return 0; } int display(){ int num = 25; return num; }
Output :
25
Explanation :
Step 1 : Array of Function
int(*arr[3])();
above statement tell that -
- arr is an array of size 3.
- Array stores address of functions
- Array stores address of function having integer as return type and does not takes any parameter.
Step 2 : Declaring Array of function Pointer
int(*(*ptr)[3])();
- It is array of function pointer which points to “array of function“.
Step 3 : Store function names inside function array
arr[0] = display; arr[1] = getch;
Step 4 : Store address of function Array to Function Pointer
ptr = &arr;
Step 5 : Calling Function
following syntax is used to call display function -
(**ptr)();
this syntax is used to call getch function -
(*(*ptr+1))();