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
Explain zero based addressing.
What are the advantages of external class?
Why is python slower than c?
Write an efficient algo and C code to shuffle a pack of cards.. this one was a feedback process until we came up with one with no extra storage.
What is c method?
How can I manipulate individual bits?
What is the difference between specifying a constant variable like with constant keyword and #define it? i.e what is the difference between CONSTANT FLOAT A=1.25 and #define A 1.25
What is the difference between malloc() and calloc()?
What are global variables and how do you declare them?
Write a program to generate the Fibinocci Series
What is the difference between a free-standing and a hosted environment?
Is there a way to jump out of a function or functions?
Explain how can I convert a string to a number?
Explain which function in c can be used to append a string to another string?
What is meant by type specifiers?