Pointer to array of function in c

Array of function means array which content is address of function and pointer 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

How to read complex pointer
Arithmetic operation with pointer
Pointer to function
Pointer to array of function
Pointer to array of string
Pointer to structure
pointer to union
Multi level pointer
Pointer to array of pointer to string
Pointer to three dimentional array
Pointer to two dimensional array
C tutorial

4 comments:

nano gowtham said...

i searched for a lot of websites to study pointer tutorial.....i ll say that this is the best site for understanding the basic concepts....thanks for posting this.....

SHAOLIN said...

ultimate explanation of pointers!!!!!
hats off to u dude.

....... said...

Best post i hav ever read.... gr8 work.... and thanks a lottttt.....

Sree said...

really very very useful for the beginners....