iDevGames Forums
Arrays or variables containing executable functions - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Arrays or variables containing executable functions (/thread-4227.html)



Arrays or variables containing executable functions - Jones - May 30, 2006 08:23 PM

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?


Arrays or variables containing executable functions - OneSadCookie - May 30, 2006 08:43 PM

Code:
typedef struct {

    void (*func)(void);

} myStruct;

myStruct fooThingy;
fooThingy.func = wootfunc;

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



Arrays or variables containing executable functions - Zekaric - May 30, 2006 10:28 PM

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.


Arrays or variables containing executable functions - Jones - Jun 1, 2006 07:18 PM

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


Arrays or variables containing executable functions - Zekaric - Jun 2, 2006 08:35 AM

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.