通过C中的数组调用函数?

use*_*512 4 c arrays function

我正在编写一个程序,它有一个运行另一个函数的函数:

int executePuzzle(int input) {
    switch (input) {
        case 1: puzzle1();break;
        case 2: puzzle2();break;
        default:break;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,简单地使用以下内容可能更有效:

int puzzle[2] = {puzzle1(),puzzle2()};
Run Code Online (Sandbox Code Playgroud)

然后调用puzzle0; 我想知道如何做到这一点.

Jar*_*Par 9

这听起来像是一个函数指针很有用的地方

typedef void (*puzzlePointer)();
puzzlePointer puzzles[] = { puzzle1, puzzle2, puzzle3 };

void executePuzzle(int input) {
  if (input >= 0 && input < 2) {
    puzzles[input]();
  }
}
Run Code Online (Sandbox Code Playgroud)