<分区>
我有一个具有以下格式的结构数组:
struct command functions[] = {
{"1", function1},
{"2", function2},
etc....
}
在函数 run(char* function) 中,我检查参数是否等同于存储在结构数组中的字符串之一。如果是,我想调用相应的函数。例如,如果传入“1”,我调用 function1()。
这将如何实现?
到目前为止,我有
run(char* function) {
for (int i = 0; i < num_functions; i++) {
if(*function == functions[i]) {
return (*function)();
}
}
}
出现以下错误:
error: invalid operands to binary == (have int and struct command)
error: called object *function is not a function