GP32 What Does This Line Of Code Do?


yaustar

UK GP32 & GP2X Owner
Joined
Oct 18, 2003
Messages
2,714
Location
UK
Website
Visit site
Right.. I sort of know what a typedef is, function pointers are okay but the two combined together I dont have a clue:
Code:
typedef void (* MyFunctionPointer)( void * lpUserData );
 
yaustar posted on Sep 2 2004 at 01:43 AM said:
Code:
typedef void (* MyFunctionPointer)( void * lpUserData );
it defines a type called "MyFunctionPointer" that can be used for function pointers of this form.. it's useful, eg. for dispatch tables.
Code:
(MyFunctionPointer c[]= {foo,bar,baz}; c[2](userdata); )
 
Last edited by a moderator:
Sorry... but I am going to be awkward and ask you to explain that in a different way... I didnt quite get it... (mainly about which "form")
 
yaustar posted on Sep 2 2004 at 12:00 AM said:
Sorry... but I am going to be awkward and ask you to explain that in a different way... I didnt quite get it... (mainly about which "form")
It's just a typedef for a function pointer, not sure what is confusing about it. :eek:

To use it you could do:

Code:
typedef void (* MyFunctionPointer)( void * lpUserData );

void *FuncToDoSomethingInteresting(void * lpUserData)
{
   void *ptr;

   // Do something here involving ptr

   return ptr;
}

void main(void)
{
   void *anotherptr;
   void *data;

   MyFunctionPointer TestFunc;

   TestFunc = FuncToDoSomethingInteresting;

   anotherptr = TestFunc(data);
}

Typedefs just make the above more readable than not using them. And of course much more useful when declaring an array of functions:

Code:
MyFunctionPointer BunchOfFuncs[100];

BunchOfFuncs[0] = FuncToDoSomethingInteresting;
anotherptr = BunchOfFuncs[0](data);

Arrays of function pointers are useful in C when you need to achieve similar results to OOP in C++.

They are also very useful when writing emulators.
 
Last edited by a moderator:
Back
Top