application of pointers to functions

Discussion in 'C Programming' started by sid, Jun 27, 2007.

  1. sid

    sid Guest

    Hi!
    can anyone please tell me the use (application) of pointers to
    functions?
    e.g. void *func()
    {
    _ _ __
    _____
    }
    sid, Jun 27, 2007
    #1
    1. Advertising

  2. sid

    Chris Dollin Guest

    sid wrote:

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

    --
    Chris "higher-order functions rule" Dollin

    Hewlett-Packard Limited registered office: Cain Road, Bracknell,
    registered no: 690597 England Berks RG12 1HN
    Chris Dollin, Jun 27, 2007
    #2
    1. Advertising

  3. On Wed, 27 Jun 2007 10:37:18 -0000, sid <>
    wrote:

    >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
    Barry Schwarz, Jun 27, 2007
    #3
  4. sid

    user923005 Guest

    On Jun 27, 3:37 am, sid <> wrote:
    > 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;
    }
    user923005, Jun 28, 2007
    #4
  5. Chris Dollin wrote:
    > sid wrote:
    >
    >> 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.
    >


    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. ;-)

    --
    +----------------------------------------------------------------+
    | Charles and Francis Richmond richmond at plano dot net |
    +----------------------------------------------------------------+
    Charles Richmond, Jun 29, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. S?ren Gammelmark
    Replies:
    1
    Views:
    1,866
    Eric Sosman
    Jan 7, 2005
  2. Marc Thrun
    Replies:
    15
    Views:
    846
    Tim Rentsch
    Oct 4, 2005
  3. newbie

    Pointers to char pointers in functions

    newbie, Sep 18, 2006, in forum: C Programming
    Replies:
    9
    Views:
    297
    August Karlstrom
    Sep 24, 2006
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    651
  5. ajaybgr
    Replies:
    18
    Views:
    946
    Philip Lantz
    Sep 7, 2012
Loading...

Share This Page