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

find a number whether it is even or odd without using any control structures and relational operators?

22 Answers   Microsoft, Shashank Private Limited,


How we can write a value to an address using macro..?

0 Answers   Tata Elxsi,


How does #define work?

0 Answers  


9.how do you write a function that takes a variable number of arguments? What is the prototype of printf () function? 10.How do you access command-line arguments? 11.what does ‘#include<stdio.h>’ mean? 12.what is the difference between #include<> and #include”…”? 13.what are # pragma staments? 14.what is the most appropriate way to write a multi-statement macro?

4 Answers   L&T,


Write a programme to find even numbers without using any conditional statement?

3 Answers   ADD Software, Infosys,






2) Write a program that will help Air Traffic Control for an airport to view the sequence of flights ready for take-off. The airport can accommodate 10 flights waiting for take-off at any point in time. Each flight has a unique 3 digit numeric identifier. &#61550; Each time a flight takes-off, Air Traffic Control adds a flight to the waitlist. Each time a flight is added to the waitlist, the list of flights waiting to take-off must be displayed. &#61550; When a flight is cleared for take-off, Air Traffic Control removes the flight from the waitlist. Each time a flight takes-off, the list of flights waiting to take-off must be displayed. &#61550; Sequence of take-off is the sequence of addition to the waitlist

0 Answers  


When would you use a pointer to a function?

0 Answers  


What is the size of a union variable?

0 Answers  


How many main () function we can have in a project?

0 Answers  


Why does this code crash?

0 Answers  


What is non linear data structure in c?

0 Answers  


write a program in c language that uses function to locate and return the smallest and largest integers in an array,number and their position in the array. it should also find and return the range of the numbers , that is , the difference between the largest number and the smallest.

1 Answers  


Categories