6 registered members (AndrewAMD, Ayumi, degenerate_762, 7th_zorro, VoroneTZ, HoopyDerFrood),
1,268
guests, and 6
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Function pointers in structs
#403016
06/13/12 15:23
06/13/12 15:23
|
Joined: Nov 2008
Posts: 946
the_clown
OP
User
|
OP
User
Joined: Nov 2008
Posts: 946
|
Alright, so I'm trying the following:
// some function
void doSomething(void)
{
draw_text("Doing something",20,20,vector(255,255,255));
}
// some struct
typedef struct someStruct
{
void myFunction(void);
}someStruct;
someStruct* myStruct =
{
myFunction = doSomething;
}
// a main void
void main()
{
while(1)
{
if(key_j)
myStruct.myFunction();
wait(1);
}
}
So, obviously I'm trying to use a function pointer as a member of my struct to be able to have different instances of that struct calling different functions. However, running this, first of all, I get a compiling error (wrong number/type of parameters) for my function call in the main function; If I replace myStruct.myFunction(); with myStruct.myFunction(NULL); it compiles, however as I press "j" I get a "crash in main: SYS" error message. So, now I'd like to know, how can I use function pointers as members of a struct, and how can I call these functions without crash? Thanks in advance.
|
|
|
Re: Function pointers in structs
[Re: the_clown]
#403017
06/13/12 15:36
06/13/12 15:36
|
Joined: Feb 2009
Posts: 3,207 Germany, Magdeburg
Rei_Ayanami
Expert
|
Expert
Joined: Feb 2009
Posts: 3,207
Germany, Magdeburg
|
AFAIK it didn't work that way. I could never use it that way either. Try this:
void main()
{
while(1)
{
if(key_j)
{
void testFunction();
testFunction = myStruct.myFunction;
testFunction();
}
wait(1);
}
}
|
|
|
Re: Function pointers in structs
[Re: Rei_Ayanami]
#403018
06/13/12 15:39
06/13/12 15:39
|
Joined: Nov 2008
Posts: 946
the_clown
OP
User
|
OP
User
Joined: Nov 2008
Posts: 946
|
Hmm, nope, same crash. Thanks though. More ideas? ^^
EDIT: Okay, it works if I create the struct at runtime, then assign the function to the pointer... this is kinda strange though, why won't it work if I assign the function to the pointer in the initialization?
Last edited by the_clown; 06/13/12 16:03.
|
|
|
Re: Function pointers in structs
[Re: the_clown]
#403355
06/18/12 18:32
06/18/12 18:32
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
Expert
|
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
// some function
void doSomething(void)
{
draw_text("Doing something",20,20,vector(255,255,255));
}
// some struct
typedef struct someStruct
{
void* myFunction;
}someStruct;
someStruct* myStruct =
{
myFunction = doSomething;
}
void prototype_doSomething(void);
// a main void
void main()
{
while(1)
{
if(key_j)
prototype_doSomething = myStruct.myFunction;
prototype_doSomething();
wait(1);
}
}
This should work. You can't call functions from structures. Structures are not classes, they are data collections. Lite-C only allows function pointers in structures. So you have to define a prototype function without body, assign the pointer from the structure to the prototype, and then call the prototype instead.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: Function pointers in structs
[Re: Petra]
#403393
06/19/12 10:19
06/19/12 10:19
|
Joined: Nov 2008
Posts: 946
the_clown
OP
User
|
OP
User
Joined: Nov 2008
Posts: 946
|
@joozey, I think your solution is the same that rei suggested, doesn't work eiter; However, I had already tried what Petra posted, and it works. It does not work if the pointer is set on a function in a struct initialization like this:
SOMESTRUCT* myStruct =
{
myFunc = someFunc;
}
If I do this, calling the function per pointer results in a SYS error, no matter if I call it direclty or call a prototype function. If I do this, however:
SOMESTRUCT* myStruct =
{
myFunc = NULL;
}
void main()
{
myStruct->myFunc = someFunc;
myStruct->myFunc();
}
then it works, just like in Petras suggestion. The issue for me is that I wanted to use a struct full of function pointers for a somewhat OOP-approach; this isn't of much use however if I have to assign the functions to the pointers in another function instead of simply declaring an instance of the struct as I would declare a PANEL or an ENTITY.
|
|
|
|