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 does void main () mean?
Describe the complexity of Binary search, Quicksort and various other sorting and searching techniques..
What is the advantage of an array over individual variables?
How can you check to see whether a symbol is defined?
Can we declare function inside main?
What is the size of structure in c?
How is a macro different from a function?
What is spark map function?
Explain how can a program be made to print the name of a source file where an error occurs?
What is atoi and atof in c?
What is a stream water?
Can we change the value of static variable in c?
How to explain the final year project as a fresher please answer with sample project
What is the best way to store flag values in a program?
What is bss in c?