问题描述
假设我有这三个函数:
bool A(); bool B(); bool C();
如何使用函数指针有条件地调用这些函数之一,以及如何声明函数指针?
推荐答案
您可以执行以下操作:假设你有你的 A,B &C函数如下:
bool A() { ..... } bool B() { ..... } bool C() { ..... }
现在在其他一些功能上,比如在 main:
int main() { bool (*choice) (); // now if there is if-else statement for making "choice" to // point at a particular function then proceed as following if ( x == 1 ) choice = A; else if ( x == 2 ) choice = B; else choice = C; if(choice()) printf("Success\n"); else printf("Failure\n"); ......... ......... }
请记住,这是函数指针的一个示例.还有其他几种方法,你必须清楚地学习函数指针.