Arrays or variables containing executable functions

Jones
Unregistered
 
Post: #1
Is it possible to have an array or a struct that contains a void/int/float value that is a function? Like saying: This slot can be filled by the name of a function.

Here's what I mean:
(Let's say I have a function called "wootfunc").

Code:
typedef struct {
[INDENT]void *func();[/INDENT]
} myStruct;

myStruct fooThingy;
fooThingy.func() = wootfunc();

// Now let's execute fooThingy's function:
fooThingy.func();

It's a bit crazy, but is it possible in some way shape or form?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Code:
typedef struct {

    void (*func)(void);

} myStruct;

myStruct fooThingy;
fooThingy.func = wootfunc;

// Now let's execute fooThingy's function:
fooThingy.func();
Quote this message in a reply
Member
Posts: 131
Joined: 2004.10
Post: #3
If you are trying to save functions with different parameters to the same pointer then you will need to do some type casting back and forth to a proper function pointer variable before calling.
Quote this message in a reply
Jones
Unregistered
 
Post: #4
I am actually making a menu structure system, the user simply declares a menu item, gives it a name and a result function, then when my auto-executing functions see it's been clicked, the function set by the user will be run.

Ingenious. Wink
Quote this message in a reply
Member
Posts: 131
Joined: 2004.10
Post: #5
This is more commonly known as a callback sort of system. Very simple to understand and works fine for the most part. There are cases where this isn't as nice but I don't see you hitting that case with your set up; being programmatically generated menu system and not resource or external file based.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Functions not being initialised onKeyDown Yendall 1 1,237 Apr 15, 2014 01:31 PM
Last Post: OneSadCookie
  C: Global Variables versus Parameters Lizard Man 10 6,419 Jan 13, 2010 08:22 PM
Last Post: Lizard Man
  Some quick help getting started with certain Carbon functions zmwworm 12 8,241 Jan 10, 2008 01:14 AM
Last Post: zmwworm
  Accessing an inherited class's variables Tobs_ 22 9,951 Feb 28, 2007 05:26 PM
Last Post: mac_girl
  Problems with variables in Obj-C vnvrymdreglage 16 6,965 Oct 2, 2006 10:19 PM
Last Post: vnvrymdreglage