What is pointer to a
function?
Explanation:
(1) What will be output if you will
execute following code?
int * function();
int main(){
auto int *x;
int *(*ptr)();
ptr=&function;
x=(*ptr)();
printf("%d",*x);
}
int *function(){
static int a=10;
return &a;
}
Output: 10
Explanation: Here function is
function whose parameter is void data type and return type is pointer to int
data type.
x=(*ptr)()
=>
x=(*&functyion)() //ptr=&function
=>
x=function() //From rule *&p=p
=> x=&a
So, *x =
*&a = a =10
(2) What will be output if you will
execute following code?
int find(char);
int(*function())(char);
int main(){
int x;
int(*ptr)(char);
ptr=function();
x=(*ptr)('A');
printf("%d",x);
return 0;
}
int find(char c){
return c;
}
int(*function())(char){
return find;
}
Output: 65
Explanation: Here function
whose name is function which passing void data type and returning another
function whose parameter is char data type and return type is int data type.
x=(*ptr)(‘A’)
=> x= (*function
()) (‘A’) //ptr=function ()
//&find=function ()
i.e. return type of function ()
=> x= (* &find)
(‘A’)
=> x= find
(‘A’) //From rule*&p=p
=> x= 65
Blog Author: Vijay Kumar
No comments:
Post a Comment