Malloc是一组函数指针

Aar*_*onF 2 c arrays function-pointers

我有一组函数需要在运行时分配才能按顺序调用.哪个函数指针以编程方式确定哪个点,例如:

void ((drawFunctions*)(...))[0] = drawTriangle;
...
for(...)
    drawFunctions[i](...);
Run Code Online (Sandbox Code Playgroud)

我想要malloc一个函数指针数组,因为直到运行时我才知道需要多少个.你会怎么做?

小智 5

typedef可能会使语法更具可忍性:

typedef void (*drawFunctionPointer)(void);
drawFunctionPointer *drawFunctions = malloc(sizeof(drawFunctionPointer) * numFunctions);
Run Code Online (Sandbox Code Playgroud)