how to declare and use an array of pointer on functions?
-
Hello people, I'd like to know how to declare and use an array of pointer on functions? Lets say:
void f1() { ... } void f2() { ... } void f3() { ... } typedef struct _toto { int x, y; //how to declare my array of pointers functions[3]; } TOTO; TOTO g_toto; void InitTOTO() { g_toto.x=g_toto.y=0; // how to initialize my array of pointers on functions? g_toto.functions[0]=f1; g_toto.functions[1]=f2; g_toto.functions[3]=f3; } int main() { InitTOTO(); for (int i=0; i<3; i++) //how to call g_toto.functions[i]() }
I hope you see what I wish to do... Thanks for giving me the clue :) Best regards. Fred. There is no spoon. -
Hello people, I'd like to know how to declare and use an array of pointer on functions? Lets say:
void f1() { ... } void f2() { ... } void f3() { ... } typedef struct _toto { int x, y; //how to declare my array of pointers functions[3]; } TOTO; TOTO g_toto; void InitTOTO() { g_toto.x=g_toto.y=0; // how to initialize my array of pointers on functions? g_toto.functions[0]=f1; g_toto.functions[1]=f2; g_toto.functions[3]=f3; } int main() { InitTOTO(); for (int i=0; i<3; i++) //how to call g_toto.functions[i]() }
I hope you see what I wish to do... Thanks for giving me the clue :) Best regards. Fred. There is no spoon.The functions need to have the same prototype (that is, same return type, same number of arguments of same types). Then you will need to typedef your prototype (just an example here):
typedef (int)MyFuncPrototype(int, int)
Then you can use your function pointer:MyFuncPrototype FuncTable[3]; FuncTable[0] = SomeFunction; .... .... FuncTable[0](5,6); // Just as example
-
The functions need to have the same prototype (that is, same return type, same number of arguments of same types). Then you will need to typedef your prototype (just an example here):
typedef (int)MyFuncPrototype(int, int)
Then you can use your function pointer:MyFuncPrototype FuncTable[3]; FuncTable[0] = SomeFunction; .... .... FuncTable[0](5,6); // Just as example
-
Hello people, I'd like to know how to declare and use an array of pointer on functions? Lets say:
void f1() { ... } void f2() { ... } void f3() { ... } typedef struct _toto { int x, y; //how to declare my array of pointers functions[3]; } TOTO; TOTO g_toto; void InitTOTO() { g_toto.x=g_toto.y=0; // how to initialize my array of pointers on functions? g_toto.functions[0]=f1; g_toto.functions[1]=f2; g_toto.functions[3]=f3; } int main() { InitTOTO(); for (int i=0; i<3; i++) //how to call g_toto.functions[i]() }
I hope you see what I wish to do... Thanks for giving me the clue :) Best regards. Fred. There is no spoon.See if this thread is of any help.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown