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 #pragma statements?
What are the restrictions of a modulus operator?
write a program to find out prime number using sieve case?
What is const and volatile in c?
What is a protocol in c?
Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned long in decimal. [ I gave the obvious solution of taking % 10 and / 10, which gives us the decimal value in reverse order. This requires an array since we need to print it out in the correct order. The interviewer wasn't too pleased and asked me to give a solution which didn't need the array ].
What is NULL pointer?
Why ca not I do something like this?
What do mean by network ?
What is adt in c programming?
What is a substring in c?
Write a program to know whether the input number is an armstrong number.
Why c is a procedural language?
What are the types of c language?
explain what is an endless loop?