application of pointers to functions

S

sid

Hi!
can anyone please tell me the use (application) of pointers to
functions?
e.g. void *func()
{
_ _ __
_____
}
 
C

Chris Dollin

sid said:
can anyone please tell me the use (application) of pointers to
functions?

I'm tempted to just point out that you didn't even bother to
google "function pointer" before posting your message. But
today I'm feeling generous.

When you want to be able to supply different functions to the same
code at different times. You can usefully think of them as function
variables.

An example right there in the C library is qsort, which takes a
pointer-to-function which tells it how to compare the items being
sorted. Since qsort sorts arrays of pretty much anything, clearly
it can't know by itself how to do the comparison.
 
B

Barry Schwarz

Hi!
can anyone please tell me the use (application) of pointers to
functions?
e.g. void *func()
{
_ _ __
_____
}

Bad example. This is not a pointer to function. It is a function
returning pointer to void.

For a specific example, look in your reference at the qsort function
which takes a pointer to function as one of its parameters.

In short, you use a pointer to function whenever you want to refer to
a function indirectly.

One reason is because the determination of which function to
call is made at run time as in:
if (...) func_ptr = func1;
else if (...) func_ptr = func2;
...
func_ptr(...);

Another reason is because a pointer to function can appear in
certain places where a function cannot, such as in the argument and
parameter lists of a function (e.g., qsort again) or as a member of a
struct as in
struct x {
int x1;
float x2;
void (*func_ptr)(int,float);};

There are probably other reasons which I haven't thought of at
the moment.

As with all "when do I use a particular language feature" questions,
the answer is when that feature of the language helps you perform the
task you are designing the software for.

Language tutorials tell you how to use a language feature but your
question is one of design.


Remove del for email
 
U

user923005

Hi!
can anyone please tell me the use (application) of pointers to
functions?
e.g. void *func()
{
_ _ __
_____



}

Callbacks:
http://en.wikipedia.org/wiki/Callback_(computer_science)

Here is a not-so-useful example of using function pointers. If you
run it through your profiler, it will tell you something about
different ways of executing lists of functions:

#include <math.h>
#include <stdio.h>

typedef double (*f_t) (double);
static f_t f[] = {log, log10, sqrt, cos, cosh, exp, sin, sinh,
tan, tanh, 0};

static double accum0 = 0;
static double accum1 = 0;
static double accum2 = 0;


void arr(void)
{
int i;
double d = 0;
for (i = 0; f; i++) {
d += f (0.5);
}
accum0 += d;
}

void poi(void)
{
f_t *flist = f;
double d = 0;
while (*flist) {
f_t ff = *flist;
d += ff(0.5);
flist++;
}
accum1 += d;
}

void swi(void)
{
int i;
double d = 0;
for (i = 0; f; i++) {
switch (i) {
case 0:
d += f[0] (0.5);
break;
case 1:
d += f[1] (0.5);
break;
case 2:
d += f[2] (0.5);
break;
case 3:
d += f[3] (0.5);
break;
case 4:
d += f[4] (0.5);
break;
case 5:
d += f[5] (0.5);
break;
case 6:
d += f[6] (0.5);
break;
case 7:
d += f[7] (0.5);
break;
case 8:
d += f[8] (0.5);
break;
case 9:
d += f[9] (0.5);
break;
default:
break;
}
}
accum2 += d;
}

int main(void)
{
long i;
for (i = 0; i < 1000000; i++) {
arr();
poi();
swi();
}
printf("%.20g, %.20g, %.20g\n", accum0, accum1, accum2);
return 0;
}
 
C

Charles Richmond

Chris said:
I'm tempted to just point out that you didn't even bother to
google "function pointer" before posting your message. But
today I'm feeling generous.

When you want to be able to supply different functions to the same
code at different times. You can usefully think of them as function
variables.

An example right there in the C library is qsort, which takes a
pointer-to-function which tells it how to compare the items being
sorted. Since qsort sorts arrays of pretty much anything, clearly
it can't know by itself how to do the comparison.

Or for specifying "call-backs", like the use of the "atexit()"
function which specifies a function to be called when the program
calls "exit()".

Function pointers can also be returned from another function.

Plus you are allowed to find your own uses for function pointers. ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top