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 |
What is mean by Data Driven framework in QTP? Can any one answer me in details on this regard.
develop algorithms to add polynomials (i) in one variable
actually i have 2 years teaching experience as computer faculty but now i am a DBA but when i go for interview many peoples asked me why i left my teaching profession and why i want to come in this field kindly give me the proper answer of this queston
main() { int i; printf("%d", &i)+1; scanf("%d", i)-1; }
for(;;) printf("C language") What is out put of above??
2 Answers Practical Viva Questions,
what is the difference between entry control and exit control statement?
12 Answers Darbari Lal DAV Model School,
How can you determine the size of an allocated portion of memory?
char ch=10;printf("%d",ch);what is the output
discuss the steps needed to get a program from source code to executable in a system?
what is data structure
enum colors {BLACK,BLUE,GREEN} main() { printf("%d..%d..%d",BLACK,BLUE,GREEN); return(1); }
what is the use of getch() function in C program.. difference b/w getch() and getche()??
29 Answers HCL, IBM, Infosys, TCS, Wipro,