functions that return pointer to function

Discussion in 'C Programming' started by msolem@linuxmail.org, Mar 4, 2006.

  1. Guest

    I have some code where there are a set of functions that return
    pointers to each other. I'm having a bit of a hard time figuring out
    the correct type to use to do that.

    The code below works but I'm defining the functions as void*, and then
    casting when I use them. This code is going into a general purpose
    framework, and it would be much nicer if the user didn't need to do
    any casting.

    Can someone tell me how to set up those typedefs so that the casting
    done in main is not required?

    Thanks,
    Mike

    =================================
    #include <stdio.h>

    typedef void* p_t;
    typedef void (*pf_t)(void);
    typedef pf_t (*ppf_t)(void);

    p_t f1(void);
    p_t f2(void);

    p_t f1(void)
    {
    printf("func1\n");
    return f2;
    }
    p_t f2(void)
    {
    printf("func2\n");
    return f1;
    }

    int main (void)
    {
    ppf_t var;

    var = (ppf_t)f1;
    var = (ppf_t)var();
    var = (ppf_t)var();
    var = (ppf_t)var();

    return 0;
    }
    =================================

    The output is:
    func1
    func2
    func1
    , Mar 4, 2006
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > I have some code where there are a set of functions that return
    > pointers to each other. I'm having a bit of a hard time figuring out
    > the correct type to use to do that.
    >
    > The code below works but I'm defining the functions as void*, and then
    > casting when I use them. This code is going into a general purpose
    > framework, and it would be much nicer if the user didn't need to do
    > any casting.
    >
    > Can someone tell me how to set up those typedefs so that the casting
    > done in main is not required?


    This eliminates your casting. If it doesn't work with your compiler, see
    Michael Mair's comments. I commentd out your original code with '#if 0'
    '#endif'. Hint: compare the new pf_t with f1.

    #include <stdio.h>

    typedef void* p_t;
    #if 0
    typedef void (*pf_t)(void);
    typedef pf_t (*ppf_t)(void);
    #endif
    typedef p_t (*pf_t)(void);

    p_t f1(void);
    p_t f2(void);

    p_t f1(void)
    {
    printf("func1\n");
    return f2;
    }
    p_t f2(void)
    {
    printf("func2\n");
    return f1;
    }

    int main (void)
    {
    #if 0
    ppf_t var;
    #endif
    pf_t var;

    #if 0
    var = (ppf_t)f1;
    var = (ppf_t)var();
    var = (ppf_t)var();
    var = (ppf_t)var();
    #endif
    var = f1;
    var = var();
    var = var();
    var = var();

    return 0;
    }


    Rod Pemberton
    Rod Pemberton, Mar 5, 2006
    #2
    1. Advertising

  3. Chris Torek Guest

    > schrieb:
    >> I have some code where there are a set of functions that return
    >> pointers to each other. I'm having a bit of a hard time figuring out
    >> the correct type to use to do that.


    In article <>
    Michael Mair <> wrote:
    >There is none. At least not in portable C.


    There is no *direct* type, at least.

    [snippage]

    >Start at f1. f1 returns a pointer to the type of f2.
    >f2 returns the type of f1, i.e. a pointer to a function
    >returning the type of f2, ....
    >The only answer is a generic function pointer type, which
    >C does not have.


    Alternatively, you can wrap these things up inside "struct"s:

    #include <stdio.h>

    struct func {
    struct func (*fp)(void);
    };

    struct func f1(void), f2(void);

    struct func f1(void) {
    struct func val;

    puts("in f1");
    val.fp = f2;
    return val;
    }

    struct func f2(void) {
    struct func val;

    puts("in f2");
    val.fp = f1;
    return val;
    }

    int main(void) {
    struct func f = { f1 };
    int i;

    for (i = 0; i < 5; i++)
    f = f.fp();
    return 0;
    }

    (or, do the same as the above but change "struct func" to
    "struct func *" in all the obvious places, make f point to one,
    and make the "val"s in each function static and return their
    addresses; or pass a pointer to the "struct func" object to
    each function and have it update f->fp; etc.).

    The OP's method (having functions return "pointer to function (args)
    returning T" for some valid type T, then using casting to make it all
    work out) is OK, if a bit error-prone. In particular, casting tends
    to make the compiler shut up even if the cast is wrong, so you have
    to very sure that every cast is really correct.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, Mar 5, 2006
    #3
  4. Guest

    Thank you everybody. This has been very helpfull.

    Mike
    , Mar 8, 2006
    #4
    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. glen stark
    Replies:
    2
    Views:
    689
    Ron Natalie
    Oct 10, 2003
  2. Fraser Ross
    Replies:
    4
    Views:
    1,026
    Fraser Ross
    Aug 14, 2004
  3. murgan
    Replies:
    6
    Views:
    4,841
    Thad Smith
    Dec 21, 2005
  4. Vijai Kalyan
    Replies:
    4
    Views:
    690
    Vijai Kalyan
    Nov 8, 2005
  5. Replies:
    3
    Views:
    299
    Philip Potter
    Apr 11, 2008
Loading...

Share This Page