Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
6 registered members (AndrewAMD, Ayumi, degenerate_762, 7th_zorro, VoroneTZ, HoopyDerFrood), 1,268 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
LucasJoshua, Baklazhan, Hanky27, firatv, wandaluciaia
19053 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Function pointers in structs #403016
06/13/12 15:23
06/13/12 15:23
Joined: Nov 2008
Posts: 946
T
the_clown Offline OP
User
the_clown  Offline OP
User
T

Joined: Nov 2008
Posts: 946
Alright, so I'm trying the following:

Code:
// 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 Offline
Expert
Rei_Ayanami  Offline
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:

Code:
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
T
the_clown Offline OP
User
the_clown  Offline OP
User
T

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 Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
Code:
// 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: Joozey] #403390
06/19/12 09:55
06/19/12 09:55
Joined: Apr 2008
Posts: 586
Austria
Petra Offline
Support
Petra  Offline
Support

Joined: Apr 2008
Posts: 586
Austria
There was already a thread about this, in lite-C structs are somewhat similar to classes. You can call functions from structs, but you must use a pointer to the struct. This works:

Code:
typedef struct
{
  void fn(char* text);    
} TEST;

void func(char* text)    //your function
{
  printf(text);
}

TEST test;

void main()
{
  TEST* t = &test;
  t->fn = func;  
  t->fn("Hello world!");   
}




Re: Function pointers in structs [Re: Petra] #403393
06/19/12 10:19
06/19/12 10:19
Joined: Nov 2008
Posts: 946
T
the_clown Offline OP
User
the_clown  Offline OP
User
T

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:

Code:
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:

Code:
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.

Re: Function pointers in structs [Re: the_clown] #403397
06/19/12 12:32
06/19/12 12:32
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
@petra right, thanks!

Good you have it to work. You could write a TEST_startup() function where you attach the function implementation to the structure laugh


Click and join the 3dgs irc community!
Room: #3dgs

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1