Pointer to array of function in c


Array of function means array which content is address of function andpointer to array of function means pointer is pointing to such array.


In other word we can say pointer to array of functions is a pointer which is pointing to an array which contents are pointers to a function.


Examples of pointer to array of function:

What will be output if you will execute following code?


#include<stdio.h>
int display();
int(*array[3])();
int(*(*ptr)[3])();

int main(){
array[0]=display;
array[1]=getch;
ptr=&array;

printf("%d",(**ptr)());

(*(*ptr+1))();
return 0;
}
int display(){
int x=5;
return x++;
}

Output: 5
Explanation:

In this example:

array []: It is array of pointer to such function which parameter is void and return type is int data type.

ptr: It is pointer to array which contents are pointer to such function which parameter is void and return type is int type data.

(**ptr)() = (** (&array)) () //ptr=&array
= (*array) () // from rule *&p=p
=array [0] () //from rule *(p+i)=p[i]
=display () //array[0]=display
(*(*ptr+1))() =(*(*&array+1))() //ptr=&array
=*(array+1) () // from rule *&p=p
=array [1] () //from rule *(p+i)=p[i]
=getch () //array[1]=getch

No comments:

Post a Comment