本示例使用函数指针数组,每个数组元素为函数名字,即函数地址,现在大部分的菜单系统都
使用类似的方法,对于要实现多项菜单选择的系统有实际参考意义。
#include <STDIO.H>
void func1( int element )
{
printf( "now you enter function 1, the element is %d/n", element );
}
void func2( int element )
{
printf( "now you enter function 2, the element is %d/n", element );
}
void func3( int element )
{
printf( "now you enter function 3, the element is %d/n", element );
}
int main()
{
int choice;
void (*funcptr[])(int) = { func1, func2, func3 };
printf( "please enter the choice from 1 to 3/n" );
do
{
scanf( "%d", &choice );
if (choice<=3 && choice>0)
{
(*funcptr[choice-1])(choice);
}
} while (choice<=3 && choice>0);
return 0;
}