gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Pointer to an array of function pointers :S

#141593 - Ruben - Thu Sep 27, 2007 9:12 am

OK. I have NO idea how I would go about doing this because my compiler's just giving me errors about arithmetic or sumthing like that but anyways... I have an const array that contains pointers to functions:
Code:
const void (*MyArray[])) {
 0, &Function1, 0, 0, 0, &Function2,
 ...
 0,
};

OK. That's fine. But what I want to do is have somthing like this:
I want a pointer variable that can point to my array of pointers. I want, for example, to be able to do this:
Code:
void* (*pFunctionArray)() = &MyArray;

Even though that's not correct... I think you get the point. Like I said, I have NO idea on how to do this!

#141594 - DekuTree64 - Thu Sep 27, 2007 9:37 am

Seems to compile ok for me if I put both asterisks inside the parentheses:
Code:
void a() {}
void b() {}
void (*array[])() = { a, b };
void (**arrayPtr)() = array;


Although I'd use a typedef like this so it's easier to tell what's going on:
Code:
typedef void (*FunctionPointer)();
FunctionPointer array[] = { a, b };
FunctionPointer *arrayPtr = array;

_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#141596 - Ruben - Thu Sep 27, 2007 9:58 am

Hehe... looks like I have to do a LOT more researching on C++ LMFAO!!!!! Hehehe!! Cheers!

#141608 - gmiller - Thu Sep 27, 2007 1:02 pm

this is basic C not C++ ...

#141609 - Ruben - Thu Sep 27, 2007 1:41 pm

...*blushes*.... lol.... hehehe... should've figured that... I guess I kinda knew... lol...

#141611 - Ruben - Thu Sep 27, 2007 2:45 pm

1 more question:
I've got myself the pointer array setup like this:
void (**_MapBG2Events)();
void (**_MapBG3Events)();

But just how would I assign a 'const void' array onto this??

#141664 - gmiller - Fri Sep 28, 2007 2:33 am

As DekutTress64 suggested:

Code:
typedef void (*FunctionPointer)();
FunctionPointer array[] = { a, b };
FunctionPointer *arrayPtr = array;

But define the array to be "const FunctionPointer array = { a, b};" this way the table is filled in at compile time. If you want it at runtime then it can't be const or up in the ROM.


Last edited by gmiller on Fri Sep 28, 2007 1:25 pm; edited 1 time in total

#141665 - Ruben - Fri Sep 28, 2007 2:44 am

Alright. I'll do that right away!