Function that returns a pointer to a function of the same signature?

Discussion in 'C Programming' started by sqweek, Jan 11, 2006.

  1. sqweek

    sqweek Guest

    Hiya. I've just been implementing a finite state machine a couple of
    times to compare the performance of different approaches. One of my
    attempts was to have a function for each state, which takes a single
    character of input and returns a function pointer to the next state.
    This presented an interesting challenge when I tried to code it. ;)
    "typedef statefn* (statefn)(int);" is the closest I've got, but of
    course you can't refer to the new type within the typedef itself.
    I ended up just returning void* and noted this implementation was
    terrible[1] compared to the switch and goto solutions, but it's nagging
    me that I didn't work out how to define the function properly.
    So... is it possible? :)
    (I found a C++ solution at http://www.gotw.ca/gotw/057.htm but nothing
    in straight C)

    [1] Mind you, my state machine only had 3 states, so the advantage of
    the function method has of jumping straight to the state code instead
    of having to iterate over states to work out which one we are in (as in
    the switch method) wouldn't have had a chance to shine. Also I expect
    much better performance could be obtained by passing the input stream
    to the function so it only has to return when it needs to change state.
     
    sqweek, Jan 11, 2006
    #1
    1. Advertising

  2. sqweek

    Ico Guest

    sqweek <> wrote:
    > Hiya. I've just been implementing a finite state machine a couple of
    > times to compare the performance of different approaches. One of my
    > attempts was to have a function for each state, which takes a single
    > character of input and returns a function pointer to the next state.
    > This presented an interesting challenge when I tried to code it. ;)
    > "typedef statefn* (statefn)(int);" is the closest I've got, but of
    > course you can't refer to the new type within the typedef itself.
    > I ended up just returning void* and noted this implementation was
    > terrible[1] compared to the switch and goto solutions, but it's nagging
    > me that I didn't work out how to define the function properly.
    > So... is it possible? :)


    Not in a simple way. This is a frequently asked question, you can find the
    FAQ answer at http://c-faq.com/decl/recurfuncp.html



    --
    :wq
    ^X^Cy^K^X^C^C^C^C
     
    Ico, Jan 11, 2006
    #2
    1. Advertising

  3. sqweek

    Charles Rapp Guest

    sqweek wrote:
    > Hiya. I've just been implementing a finite state machine a couple of
    > times to compare the performance of different approaches. One of my
    > attempts was to have a function for each state, which takes a single
    > character of input and returns a function pointer to the next state.
    > This presented an interesting challenge when I tried to code it. ;)
    > "typedef statefn* (statefn)(int);" is the closest I've got, but of
    > course you can't refer to the new type within the typedef itself.
    > I ended up just returning void* and noted this implementation was
    > terrible[1] compared to the switch and goto solutions, but it's nagging
    > me that I didn't work out how to define the function properly.
    > So... is it possible? :)
    > (I found a C++ solution at http://www.gotw.ca/gotw/057.htm but nothing
    > in straight C)
    >
    > [1] Mind you, my state machine only had 3 states, so the advantage of
    > the function method has of jumping straight to the state code instead
    > of having to iterate over states to work out which one we are in (as in
    > the switch method) wouldn't have had a chance to shine. Also I expect
    > much better performance could be obtained by passing the input stream
    > to the function so it only has to return when it needs to change state.


    See http://smc.sourceforge.net which generates state machine code for a
    number of languages including C. It uses a simple language to specify
    the state machine - but this simple language supports a number of UML
    state design ideas.

    Charles Rapp
     
    Charles Rapp, Jan 13, 2006
    #3
    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. JJBW
    Replies:
    1
    Views:
    10,456
    Joerg Jooss
    Apr 24, 2004
  2. shaun roe
    Replies:
    5
    Views:
    334
    Daniel T.
    Jan 23, 2006
  3. Gustavo Narea
    Replies:
    14
    Views:
    887
    Gustavo Narea
    Feb 16, 2009
  4. daniel rich
    Replies:
    5
    Views:
    325
    daniel rich
    Nov 14, 2012
  5. kurt krueckeberg
    Replies:
    1
    Views:
    346
    Victor Bazarov
    Apr 29, 2013
Loading...

Share This Page