what is a function pointer and how all to declare ,define
and implement it ???
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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..?
How does #define work?
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?
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.  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.  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.  Sequence of take-off is the sequence of addition to the waitlist
When would you use a pointer to a function?
What is the size of a union variable?
How many main () function we can have in a project?
Why does this code crash?
What is non linear data structure in c?
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.