Program to show Pointer to function in c programming

Program to show Pointer to function in c programming

int * function();

void 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);

void main(){

int x;

int(*ptr)(char);

ptr=function();

x=(*ptr)('A');

printf("%d",x);

}

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?

char * call(int *,float *);

void main(){

char *string;

int a=2;

float b=2.0l;

char *(*ptr)(int*,float *);

ptr=&call;

string=(*ptr)(&a,&b);

printf("%s",string);

}

char *call(int *i,float *j){

char *str="lernc.blogspot.com";

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

return str;

}

Output: lernc.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)

=”lernc.blogspot.com” + *&a+ (int) (*&b)

//i=&a, j=&b

=”lernc.blogspot.com” + a+ (int) (b)

=”lernc.blogspot.com” +2 + (int) (2.0)

=”lernc.blogspot.com” +4

=”lernc.blogspot.com”

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

char far * display(char far*);

void main(){

char far* string=lernc.blogspot.com";

char far *(*ptr)(char far *);

ptr=&display;

string=(*ptr)(string);

printf("%s",string);

}

char far *display(char far * str){

char far * temp=str;

temp=temp+13;

*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+13

temp=’\0’

Above two lines replaces first dot character by null character of string of variable string i.e.

"lernc\0blogspot.com"

As we know %s print the character of stream up to null character.

Related Links :

No comments:

Post a Comment


If you face any Problem in viewing code such as Incomplete "For Loops" or "Incorrect greater than or smaller" than equal to signs then please collect from My Web Site CLICK HERE


More Useful Topics...

 

History Of C..

In the beginning was Charles Babbage and his Analytical Engine, a machine
he built in 1822 that could be programmed to carry out different computations.
Move forward more than 100 years, where the U.S. government in
1942 used concepts from Babbage’s engine to create the ENIAC, the first
modern computer.
Meanwhile, over at the AT&T Bell Labs, in 1972 Dennis Ritchie was working
with two languages: B (for Bell) and BCPL (Basic Combined Programming
Language). Inspired by Pascal, Mr. Ritchie developed the C programming
language.

My 1st Program...


#include
#include
void main ()
{
clrscr ();
printf ("\n\n\n\n");
printf ("\t\t\t*******Pankaj *******\n");
printf ("\t\t\t********************************\n");
printf ("\t\t\t\"Life is Good...\"\n");
printf ("\t\t\t********************************");
getch ();
}

Next Step...


#include
#include

void main ()
{
clrscr ();
printf ("\n\n\n\n\n\n\n\n");
printf ("\t\t\t --------------------------- \n\n");

printf ("\t\t\t | IGCT, Info Computers, INDIA | \n\n");
printf ("\t\t\t --------------------------- ");

getch ();

}

Hits!!!