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
Why can’t we compare structures?
How can I write data files which can be read on other machines with different word size, byte order, or floating point formats?
What is the difference between break and continue?
What is static function in c?
What is the newline escape sequence?
How can I check whether a file exists? I want to warn the user if a requested input file is missing.
Explain what does a function declared as pascal do differently?
Hai sir, I had planned to write the NIC scientific engineer exam , plz post the sample question......
What are the advantages of using macro in c language?
Write the test cases for checking a variable having value in range -10.0 to +10.0?
What is the translation phases used in c language?
Explain what are run-time errors?
What is a union?
What are the advantages of the functions?
Here is a neat trick for checking whether two strings are equal