what is a function pointer and how all to declare ,define
and implement it ???
Answer Posted / 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 |
Post New Answer View All Answers
What is string length in c?
How can I call fortran?
Difference between exit() and _exit() function?
Can a pointer be null?
Differentiate between calloc and malloc.
Write a program in c to replace any vowel in a string with z?
Write a program to find factorial of a number using recursive function.
What is #include stdlib h?
What is the right type to use for boolean values in c?
Write a program to identify if a given binary tree is balanced or not.
Explain built-in function?
What are type modifiers in c?
Is c easier than java?
Explain the use of bit fieild.
How can I get the current date or time of day in a c program?