Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

abstract function pointers

How would I create an array of ten function pointers? What I have is a for loop, and I want to set a function pointer to a different function on each iteration. so:

//pseudocode
for i (0..10)
    function = array_of_functions[i];
//...
like image 479
user105033 Avatar asked Dec 03 '22 14:12

user105033


1 Answers

// Define alias for function pointer type for convenience
typedef void (*action)(int);

// Example function
void print(int) { ... }

action fs[10] = { print, ... };
for (int i = 0; i < 10; ++i)
{
    action f = fs[i];

    // Call it somehow
    f(i * i);
}
like image 69
Pavel Minaev Avatar answered Jan 18 '23 09:01

Pavel Minaev