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


Please Help Members By Posting Answers For Below Questions

What is #pragma statements?

815


What are the restrictions of a modulus operator?

829


write a program to find out prime number using sieve case?

1840


What is const and volatile in c?

772


What is a protocol in c?

737


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 ].

861


What is NULL pointer?

852


Why ca not I do something like this?

766


What do mean by network ?

874


What is adt in c programming?

835


What is a substring in c?

776


Write a program to know whether the input number is an armstrong number.

885


Why c is a procedural language?

791


What are the types of c language?

765


explain what is an endless loop?

813