what is the advantage of function pointer

Answer Posted / vastram naik

Answer:
Pointers to functions are interesting when you pass them to
other functions. A function that takes function
pointers says, in effect, “Part of what I do can be
customized. Give me a pointer to a function, and I’ll call
it when that part of the job needs to be done. That function
can do its part for me.” This is known as a
“callback.” It’s used a lot in graphical user interface
libraries, in which the style of a display is built into the
library but the contents of the display are part of the
application.
As a simpler example, say you have an array of character
pointers (char*s), and you want to sort it by the value
of the strings the character pointers point to. The standard
qsort() function uses function pointers to
perform that task. (For more on sorting, see Chapter III,
“Sorting and Searching Data.”) qsort() takes four
arguments,
u a pointer to the beginning of the array,
u the number of elements in the array,
u the size of each array element, and
u a comparison function,
and returns an int.
The comparison function takes two arguments, each a pointer
to an element. The function returns 0 if the
pointed-to elements compare equal, some negative value if
the first element is less than the second, and some
positive value if the first element is greater than the
second. A comparison function for integers might look
like this:
int icmp( const int *p1, const int *p2 )
{
return *p1 - *p2;
}
C Programming: 146 Just the FAQs
The sorting algorithm is part of qsort(). So is the exchange
algorithm; it just copies bytes, possibly by calling
memcpy() or memmove(). qsort() doesn’t know what it’s
sorting, so it can’t know how to compare them. That
part is provided by the function pointer.
You can’t use strcmp() as the comparison function for this
example, for two reasons. The first reason is that
strcmp()’s type is wrong; more on that a little later. The
second reason is that it won’t work. strcmp() takes
two pointers to char and treats them as the first characters
of two strings. The example deals with an array
of character pointers (char*s), so the comparison function
must take two pointers to character pointers
(char*s). In this case, the following code might be an
example of a good comparison function:
int strpcmp( const void *p1, const void *p2 )
{
char * const *sp1 = (char * const *) p1;
char * const *sp2 = (char * const *) p2;
return strcmp( *sp1, *sp2 );
}
The call to qsort() might look something like this:
qsort( array, numElements, sizeof( char * ), pf2 );
qsort() will call strpcmp() every time it needs to compare
two character pointers (char*s).
Why can’t strcmp() be passed to qsort(), and why were the
arguments of strpcmp() what they were?
A function pointer’s type depends on the return type of the
pointed-to function, as well as the number and
types of all its arguments. qsort() expects a function that
takes two constant void pointers:
void qsort( void *base,
size_t numElements,
size_t sizeOfElement,
int (*compFunct)( const void *, const void *) );
Because qsort() doesn’t really know what it’s sorting, it
uses a void pointer in its argument (base) and in
the arguments to the comparison function. qsort()’s void*
argument is easy; any pointer can be converted
to a void* without even needing a cast. The function pointer
is harder.
For an array of character arrays, strcmp() would have the
right algorithm but the wrong argument types. The
simplest, safest way to handle this situation is to pass a
function that takes the right argument types for
qsort() and then casts them to the right argument types.
That’s what strpcmp() does.
If you have a function that takes a char*, and you know that
a char* and a void* are the same in every
environment your program might ever work in, you might cast
the function pointer, rather than the pointedto
function’s arguments, in this way:
char table[ NUM_ELEMENTS ][ ELEMENT_SIZE ];
/* ... */
/* passing strcmp() to qsort for array of array of char */
qsort( table, NUM_ELEMENTS, ELEMENT_SIZE,
( int (*)( const void *, const void * ) ) strcmp );
Casting the arguments and casting the function pointer both
can be error prone. In practice, casting the
function pointer is more dangerous.
The basic problem here is using void* when you have a
pointer to an unknown type. C++ programs sometime
solve this problem with templates.

Is This Answer Correct ?    21 Yes 9 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is meant by type specifiers?

647


Write a program which returns the first non repetitive character in the string?

588


What is the difference between pure virtual function and virtual function?

636


general for is %wd,f-d; in this system "w" means a) 'w' represent total width of digits b) 'w' represent width which includes the digits before,after decimal place and the decimal point c) 'w' represent width which includes the digits before only d) 'w' represent width after decimal place only

571


the real constant in c can be expressed in which of the following forms a) fractional form only b) exponential form only c) ascii form only d) both a and b

1890






Explain can the sizeof operator be used to tell the size of an array passed to a function?

581


Can we access the array using a pointer in c language?

551


How can you find the day of the week given the date?

602


Can stdout be forced to print somewhere other than the screen?

603


Explain how can you be sure that a program follows the ansi c standard?

846


What is new line escape sequence?

790


What is %s and %d in c?

576


What is the use of extern in c?

633


What will be the outcome of the following conditional statement if the value of variable s is 10?

746


What is the difference between fread buffer() and fwrite buffer()?

659