Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Function that returns a pointer to a function of the same signature?

Reply
Thread Tools

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

 
 
sqweek
Guest
Posts: n/a
 
      01-11-2006
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.

 
Reply With Quote
 
 
 
 
Ico
Guest
Posts: n/a
 
      01-11-2006
sqweek <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Charles Rapp
Guest
Posts: n/a
 
      01-13-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Never ever use a raw pointer when a smart pointer can do the same job Hicham Mouline C++ 100 08-25-2009 05:07 PM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM
A function that returns a pointer on a function Boris Sargos C++ 3 04-23-2004 05:37 PM



Advertisments