C - Pointers - Discussion

You Are Here :: Home > C > Pointers - Discussion

 



Q.

What will be output if you will execute following code?

#include
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++;
}

A. 3 B. 5
C. 6 D. 7

Answer: Option B
Explaination:

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



Discussion

Your Comments Goes here...
NameDiscussion