C语言根据函数名调用对应的函数

通过函数指针定义,调用时加上参数

struct Command {
const char *name;
const char *desc;
// return -1 to force monitor to exit
int (*func)(int argc, char** argv, struct Trapframe* tf);
};

static struct Command commands[] = {
{ "help", "Display this list of commands", mon_help },
{ "kerninfo", "Display information about the kernel", mon_kerninfo },
{ "backtrace", "Display the current stack trace", mon_backtrace },
};

if (strcmp(argv[0], commands[i].name) == 0)
return commands[i].func(argc, argv, tf);

原文地址:https://www.cnblogs.com/yanchengwang/p/5459016.html