what is a function pointer and how all to declare ,define
and implement it ???

Answers were Sorted based on User's Feedback



what is a function pointer and how all to declare ,define and implement it ???..

Answer / abdur rab

A pointer variable which holdes the address of a function
is a function pointer.

eg:
declaration of function pointer

void (*function_name)( int, int ) = NULL;

defining a function

void sum ( int x, int y )
{
printf ( "\nThe sum :%d", x + y );
}

void difference ( int x, int y )
{
printf ( "\nThe difference :%d", x - y );
}

using the function pointer in the place of function.
Remember to use the same prototype as declared.

int main ( int argc, char* argv [] )
{
function_name = sum; //short way of doing
function_name = ∑ // best practice

function_name ( 10, 20 ); //short way of doing
(*function_name) ( 10, 20 ); //best practice

function_name = &difference; //best practice
(*function_name) ( 10, 20 ); //best practice

return ( 0 );
}

output
======

The sum :30
The difference :-10

Is This Answer Correct ?    9 Yes 1 No

what is a function pointer and how all to declare ,define and implement it ???..

Answer / satish

Function pointer:
a function can be called not only by
its name,but also by other name which is called function
pointer.

void fact(int);
void main()
{
void(*p)(int);
printf("Enter n\n");
scanf("%d",&n);
p=fact;
fact(n);/*normal calling a function*/
(*p)(n); /*fn calling using function pointer*/

}
void (*p)(int n)
{
int ans=1;
while(n>0)
{
ans*=n--;
}

printf(" %d != %d",n,ans);

Is This Answer Correct ?    3 Yes 4 No

what is a function pointer and how all to declare ,define and implement it ???..

Answer / swagatika

function pointer is function returning a pointer
Example-

int *sum(int a, int b)
{
int x;
x= a+b;
return &x;
}

but the pointer to a function means a function interm of
pointer pointing to the another function.

int (*sum)(sum1);//pointer to a function

Is This Answer Correct ?    0 Yes 1 No

what is a function pointer and how all to declare ,define and implement it ???..

Answer / mathiyazhagan

A function can itself stored in a memory address.By calling
the address ,instead of function name,we can invoke
function.
Eg.:
#include <stdio.h>
void sum(int,int);
{
void (*fp)(); //() denotes pointer to a function
fp=sum(); // no need & .reason : same of array
fp(10,20); //invoking function
}
void sum(int x,int y)
{
printf("sum of x%d and %d is =%d",x,y,x+y);
}

Is This Answer Correct ?    1 Yes 5 No

Post New Answer

More C Interview Questions

Explain what are header files and explain what are its uses in c programming?

0 Answers  


plz answer..... a program that reads non-negative integer and computes and prints its factorial

2 Answers  


In which mode we open the file for read,write and append also in c ? a)W b)w+ c)r+ d)a

2 Answers   BitWise,


If the size of int data type is two bytes, what is the range of signed int data type?

0 Answers  


write an algorithm to get a sentence and reverse it in the following format: input : I am here opuput: Here Am I note: first letter of every word is capiatlised

3 Answers  






Determine the code below, tell me exactly how many times is the operation sum++ performed ? for ( i = 0; i < 100; i++ ) for ( j = 100; j > 100 - i; j--) sum++;

5 Answers   ITCO, Wipro,


sum of two integers values only other then integer it should print invalid input.

1 Answers  


declare afunction pointer to int printf(char *)?

1 Answers   HCL,


What is the purpose of realloc()?

0 Answers  


Write a C program that reads a series of strings and prints only those ending in "ed"

2 Answers   Accenture,


sqrt(x+sqrt(x+sqrt(x+sqrt(x))))=2; Find the value of x?

4 Answers   Subex,


output for following code??? main() { int x=2,y,z; x*=3+2; printf("1.%d\n",x); x*=y=z=4; printf("2.%d %d %d\n",x,y,z); x=y==z; printf("3.%d\n",x); x==(y=z); printf("%d",x); }

2 Answers   Elysium,


Categories