Pointer to function in c programming | CTechnotips


Function pointer definition:  A pointer which keeps address of a function is known as function pointer.

Examples of function pointers in c: 

(1) What will be output if you will execute following code?

#include<stdio.h>
int * function();
int main(){
auto int *x;
int *(*ptr)();

ptr=&function;
x=(*ptr)();
printf("%d",*x);


return 0;
}
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?


#include<stdio.h>
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

(3) What will be output if you will execute following code?


#include<stdio.h>
char * call(int *,float *);
int main(){

char *string;
int a=2;
float b=2.0l;
char *(*ptr)(int*,float *);

ptr=&call;
string=(*ptr)(&a,&b);
printf("%s",string);


return 0;
}

char *call(int *i,float *j){
static char *str="ctechnotips.blogspot.com";

str=str+*i+(int)(*j);
return str;
}

Output: hnotips.blogspot.com

Explanation: Here call is function whose return type is pointer to character and one parameter is pointer to int data type and second parameter is pointer to float data type and ptr is pointer to such function.
str= str+*i+ (int) (*j)
=”ctechnotips.blogspot.com” + *&a+ (int) (*&b)
//i=&a, j=&b
=”ctechnotips.blogspot.com” + a+ (int) (b)
=”ctechnotips.blogspot.com” +2 + (int) (2.0)
=”ctechnotips.blogspot.com” +4
=”hnotips.blogspot.com”

(4) What will be output if you will execute following code?


#include<stdio.h>
char far * display(char far*);
int main(){

char far* string="ctechnotips.blogspot.com";
char far *(*ptr)(char far *);

ptr=&display;
string=(*ptr)(string);
printf("%s",string);


return 0;
}
char far *display(char far * str){

char far * temp=str;

temp=temp+11;
*temp='\0';

return str;
}

Output: cquestionbak

Explanation: Here display is function whose parameter is pointer to character and return type is also pointer to character and ptr is its pointer.
temp is char pointer
temp=temp+11
temp=’\0’
Above two lines replaces first dot character by null character of string of variable string i.e.
"ctechnotips\0blogspot.com"
As we know %s print the character of stream up to null character.

0 comments:

Post a Comment