求教函数指针问题
3.通过函数指针数组高效的实现,输入0,输出hello
输入1,输出world
输入2,输出c++
输入3,输出asm
输入其它输出:出错
程序代码:#include <stdio.h>
enum FUNC_E{
MSG_HELLO_E = 0,
MSG_WORLD_E,
MSG_CPLUSPLUS_E,
MSG_ASM_E,
MSG_ERROR_E,
MSG_MAX_E,
};
#define my_print(string) \
printf("%s\n",string)
typedef void(*FUNC_PTR)(void);
typedef struct __ShowList {
FUNC_PTR callback[MSG_MAX_E];
}ShowList;
void msg_hello(void)
{
my_print("hello");
}
void msg_world(void)
{
my_print("world");
}
void msg_cplusplus(void)
{
my_print("c++");
}
void msg_asm(void)
{
my_print("asm");
}
void msg_dump(void)
{
my_print("error");
}
ShowList cc_list = {
msg_hello,
msg_world,
msg_cplusplus,
msg_asm,
msg_dump
};
int main(int argc,char *argv[])
{
cc_list.callback[MSG_HELLO_E]();
cc_list.callback[MSG_ASM_E]();
cc_list.callback[MSG_ERROR_E]();
getchar();
return 0;
}希望对你有点帮助
