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

Explain zero based addressing.

595


What does %c do in c?

578


List the difference between a While & Do While loops?

623


What is the difference between a free-standing and a hosted environment?

632


What are the valid places to have keyword “break”?

642






Why void main is used in c?

555


Why does this code crash?

606


Is this program statement valid? INT = 10.50;

677


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.

1718


How can you allocate arrays or structures bigger than 64K?

673


What is merge sort in c?

632


Not all reserved words are written in lowercase. TRUE or FALSE?

712


What does a function declared as pascal do differently?

595


What is function prototype?

601


What is an auto keyword in c?

631