2 registered members (henrybane, 1 invisible),
775
guests, and 3
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
is possible lite-C`struct contain any functions
#340320
09/03/10 08:31
09/03/10 08:31
|
Joined: Dec 2009
Posts: 128 China
frankjiang
OP
Member
|
OP
Member
Joined: Dec 2009
Posts: 128
China
|
is possible lite-C`struct contain any functions --------------------------------------------------------------- void func(){ printf("hello world"); } typedef struct{ var x; var y; int (*func)(int,int); }SPOT; ---------------------------------------------------------------
development 3d game is interesting!
|
|
|
Re: is possible lite-C`struct contain any functions
[Re: Tobias]
#340324
09/03/10 08:43
09/03/10 08:43
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
This should work:
void func()
{
printf("hello world");
}
typedef struct
{
var x;
var y;
int func();
} SPOT;
// ...
SPOT spot;
spot.func = func;
//...
spot.func();
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: is possible lite-C`struct contain any functions
[Re: WretchedSid]
#340325
09/03/10 09:08
09/03/10 09:08
|
Joined: Dec 2009
Posts: 128 China
frankjiang
OP
Member
|
OP
Member
Joined: Dec 2009
Posts: 128
China
|
Error in 'MAIN' line 21: Call function @temp_00093 error< spot.func();>
development 3d game is interesting!
|
|
|
Re: is possible lite-C`struct contain any functions
[Re: frankjiang]
#340329
09/03/10 09:34
09/03/10 09:34
|
Joined: Nov 2007
Posts: 2,568 Germany, BW, Stuttgart
MasterQ32
Expert
|
Expert
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
|
you need to do it like this:Adjust the font size Make textarea smaller Make textarea bigger
typedef struct
{
var x;
var y;
void fn(var x); //The function pointer
} SPOT;
void func(var x) //your function
{
error("Hello world!");
}
SPOT* setup_spot() //a init routine
{
SPOT* spot = sys_malloc(sizeof(SPOT));
spot->x = 0;
spot->y = 0;
spot->fn = func; //Set fn as func
return spot;
}
void main()
{
SPOT* spot = setup_spot(); //create SPOT
spot->fn(5); //call func();
}
EDIT: Sorry, old code won't work! don't know why code doesn't work without parameters... with params it works
Last edited by Richi007; 09/03/10 09:43.
|
|
|
Re: is possible lite-C`struct contain any functions
[Re: MasterQ32]
#340331
09/03/10 09:43
09/03/10 09:43
|
Joined: Dec 2009
Posts: 128 China
frankjiang
OP
Member
|
OP
Member
Joined: Dec 2009
Posts: 128
China
|
Error in 'MAIN' line 21: '@temp_00716': wrong number/type of parameters<spot->fn(); //call func();>
development 3d game is interesting!
|
|
|
Re: is possible lite-C`struct contain any functions
[Re: frankjiang]
#340332
09/03/10 09:44
09/03/10 09:44
|
Joined: Nov 2007
Posts: 2,568 Germany, BW, Stuttgart
MasterQ32
Expert
|
Expert
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
|
changed post!
typedef struct
{
var x;
var y;
void fn(var x); //The function pointer
} SPOT;
void func(var x) //your function
{
error("Hello world!");
}
SPOT* setup_spot() //a init routine
{
SPOT* spot = sys_malloc(sizeof(SPOT));
spot->x = 0;
spot->y = 0;
spot->fn = func; //Set fn as func
return spot;
}
void main()
{
SPOT* spot = setup_spot(); //create SPOT
spot->fn(5); //call func();
}
don't know why code doesn't work without parameters... with params it works
|
|
|
Re: is possible lite-C`struct contain any functions
[Re: MasterQ32]
#340333
09/03/10 09:45
09/03/10 09:45
|
Joined: Dec 2009
Posts: 128 China
frankjiang
OP
Member
|
OP
Member
Joined: Dec 2009
Posts: 128
China
|
Error in 'MAIN' line 21: '@temp_00716': wrong number/type of parameters < spot->fn(); //call func(); >
development 3d game is interesting!
|
|
|
Re: is possible lite-C`struct contain any functions
[Re: frankjiang]
#340336
09/03/10 10:02
09/03/10 10:02
|
Joined: Jan 2004
Posts: 3,023 The Netherlands
Helghast
Expert
|
Expert
Joined: Jan 2004
Posts: 3,023
The Netherlands
|
typedef struct
{
var x;
var y;
STRING *funcName; //The function pointer
} SPOT;
void callingFunction() //your function
{
error("Hello world!");
}
SPOT* setup_spot() //a init routine
{
SPOT* spot = sys_malloc(sizeof(SPOT));
spot->x = 0;
spot->y = 0;
spot->funcName = str_create(""); //Set fn as func
str_cpy(spot->funcName, "callingFunction");
return(spot);
}
// empty prototype function
void PrototypeCallFunction();
void main()
{
SPOT* spot = setup_spot(); //create SPOT
PrototypeCallFunction = engine_getscript(_chr(spot->funcName));
if(PrototypeCallFunction) { PrototypeCallFunction(); }
}
Tested, and works. regards,
|
|
|
|