about struct function pointer

Posted By: frankjiang

about struct function pointer - 08/07/12 04:46

can lite_c`s struct save function point,any one know know?likes this,how can do this:

Code:
void*_functionPoint(int id,ENTITY* e){
   //...
}
typedef struct{
   int _beep;
   void* functionPoint(int id,ENTITY* e);
};


Posted By: MasterQ32

Re: about struct function pointer - 08/07/12 06:34

Code:
typedef struct 
{
    void *fnPtr;
} MYSTRUCT;

void MYSTRUCT_fnPtr(ENTITY* ent);

void assign_func(ENTITY* ent)
{
ent.x = 256;
}

MYSTRUCT str;

[...]

str.fnPtr = assign_func;

[...]
MYSTRUCT_fnPtr = str.fnPtr;
MYSTRUCT_fnPtr(you);


Posted By: krial057

Re: about struct function pointer - 08/07/12 09:30

@MasterQ32
Afaik, this will not work in Lite-c
You have to indicate the parameters in the function declaration inside the struct too.
Further more, when assigning a function pointer, the struct has to be a pointer too:

Php Code:
typedef struct 
{
	void *fnPtr(ENTITY* ent);
} MYSTRUCT;

void assign_func(ENTITY* ent)
{
	printf("test");
}

MYSTRUCT str;

function main()
{
	MYSTRUCT* temp = &str;
	temp->fnPtr = assign_func;
	temp->fnPtr(NULL);
} 




regards Alain
Posted By: MasterQ32

Re: about struct function pointer - 08/07/12 09:31

it works, because i just store the function pointer and not a prototype
for this i'm taking just some empty prototype and fill it with the correct funciton ptr
Posted By: krial057

Re: about struct function pointer - 08/07/12 10:14

Oh, true. You are right wink
© 2024 lite-C Forums