Function Pointers

Hi all!

Ive recently got into function pointers, i find that they can be quite handy for making your program very 'dynamic'.

However, the syntax is very confusing for what i want to do
This is what i want to do

I want to hold function pointers inside an array, but this array is dynamically allocated ( malloc, realloc, etc )

This is the current syntax ive come up with, but i dont think it is correct

void ( **drawFunc ) ( void*, SDL_Surface* );

Many thanks,
Super_Stinger
Use std::function in this case. It helps clarify the syntax.
I agree with @NoXzema - std::function is what you want.

However, if you insist on using normal function pointers (because you are actually using C), I suggest a typedef: i.e. say you have a function pointer:
typedef void (*DrawFuncPtr)(void*, SDL_Surface*);

Then you can declare your function pointers like this:
DrawFuncPtr drawFunc;

And hence dynamically allocate the arrays like this:
DrawFuncPtr* funcs = (DrawFuncPtr*)malloc(sizeof(DrawFuncPtr) * arraySize);

Of course, only do this if you are programming in C (which you appear to be). If you are programming in C++, then use new instead of malloc, find an alternative for passing void pointers, and use std::function.
Last edited on
Topic archived. No new replies allowed.