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 does %c do in c?
List the difference between a While & Do While loops?
What is the difference between a free-standing and a hosted environment?
What are the valid places to have keyword “break”?
Why void main is used in c?
Why does this code crash?
Is this program statement valid? INT = 10.50;
4-Take two sets of 5 numbers from user in two arrays. Sort array 1 in ascending and array 2 in descending order. Perform sorting by passing array to a function mySort(array, sortingOrder). Then multiply both the arrays returned from function, using metric multiplication technique in main. Print result in metric format.
How can you allocate arrays or structures bigger than 64K?
What is merge sort in c?
Not all reserved words are written in lowercase. TRUE or FALSE?
What does a function declared as pascal do differently?
What is function prototype?
What is an auto keyword in c?