如何使用函数指针调用函数?

假设我有这三个function:

bool A(); bool B(); bool C(); 

如何使用函数指针有条件地调用其中一个函数,如何声明函数指针?

您可以执行以下操作:假设您具有以下A,B和Cfunction:

 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"); ......... ......... } 

记住这是函数指针的一个例子。 还有其他几种方法,你必须清楚地学习function指针。

像这样声明你的函数指针:

 bool (*f)(); f = A; f(); 

最初定义一个函数指针数组,它接受一个void并返回一个void。 假设你的函数正在取消空白并返回空白。

 typedef void (*func_ptr)(void); 

现在你可以用它来创建这些函数的函数指针变量。 如下:

 func_ptr array_of_fun_ptr[3]; 

现在将函数的地址存储在三个变量中。

 array_of_fun_ptr[0]= &A; array_of_fun_ptr[1]= &B; array_of_fun_ptr[2]= &C; 

现在你可以使用函数指针调用这些函数,如下所示:

 some_a=(*(array_of_fun_ptr[0]))(); some_b=(*(array_of_fun_ptr[1]))(); some_c=(*(array_of_fun_ptr[2]))(); 

我认为你的问题已经得到了足够的回答,但是明确地指出给定一个函数指针可能是有用的

 void (*pf)(int foo, int bar); 

这两个电话

 pf(1, 0); (*pf)(1, 0); 

根据定义,各方面都完全相同。 选择使用哪个取决于您,尽管保持一致是个好主意。 很长一段时间,我更喜欢(*pf)(1, 0)因为在我看来它更好地反映了pf的类型,但是在过去的几年里我已经切换到pf(1, 0)

您可以按如下方式声明函数指针:

 bool (funptr*)(); 

这说我们正在声明一个函数指针,该函数指向一个不带任何东西的函数并返回一个bool。

下一个作业:

 funptr = A; 

要使用函数指针调用该函数:

 funptr(); 
 bool (*FuncPtr)() FuncPtr = A; FuncPtr(); 

如果要有条件地调用其中一个函数,则应考虑使用函数指针数组 。 在这种情况下,你有3个指向A,B和C的元素,你可以根据数组的索引调用一个元素,例如f的Auncarray0。

 bool (*fptr)(); int main(void) { ... ... printf("Enter your choice"); scanf("%d",&a); switch(a) { case 0: fptr = A; break; case 1: fptr = B; break; case 2: fptr = C; break; case 3: break; } (*fptr)(); return 0; } 

你的选择存储在一个; 然后,在函数指针中分配函数。 最后,根据您的选择,调用相同的函数来返回所需的结果。

请注意,当你说:

 bool (*a)(); 

你声明a类型“指向函数返回bool并获取一个未指定数量的参数的指针”。 假设定义了bool (也许你正在使用C99并且包含了stdbool.h ,或者它可能是一个typedef),这可能是也可能不是你想要的。

这里的问题是编译器现在无法检查是否将a分配给正确的值。 您的函数声明存在同样的问题。 A()B()C()都被声明为函数“返回bool并获取未指定数量的参数”。

要查看可能存在的问题,让我们编写一个程序:

 #include  int test_zero(void) { return 42; } static int test_one(char *data) { return printf("%s\n", data); } int main(void) { /* a is of type "pointer to function returning int and taking unspecified number of parameters */ int (*a)(); /* b is of type "pointer to function returning int and taking no parameters */ int (*b)(void); /* This is OK */ a = test_zero; printf("a: %d\n", a()); a = test_one; /* OK, since compiler doesn't check the parameters */ printf("a: %d\n", a()); /* oops, wrong number of args */ /* This is OK too */ b = test_zero; printf("b: %d\n", b()); /* The compiler now does type checking, and sees that the assignment is wrong, so it can warn us */ b = test_one; printf("b: %d\n", b()); /* Wrong again */ return 0; } 

当我用gcc编译上面的内容时,它说:

警告:从不兼容的指针类型分配

对于线b = test_one; ,这很好。 对a的相应赋值没有警告。

所以,你应该将你的函数声明为:

 bool A(void); bool B(void); bool C(void); 

然后保存函数的变量应声明为:

 bool (*choice)(void); 

阅读的最佳方式是“顺时针/螺旋规则”

大卫安德森

http://c-faq.com/decl/spiral.anderson.html

您为函数的给定签名声明一个函数指针变量,如下所示:

 bool (* fnptr)(); 

你可以为它分配一个function:

 fnptr = A; 

你可以称之为:

 bool result = fnptr(); 

您可以考虑使用typedef为您需要的每个不同的函数签名定义类型。 这将使代码更易于阅读和维护。 即对于返回没有参数的bool的函数的签名,这可能是:

 typdef bool (* BoolFn)(); 

然后你可以像这样用来声明这个类型的函数指针变量:

 BoolFn fnptr; 

略有不同的方法:

 bool A() {...} bool B() {...} bool C() {...} int main(void) { /** * Declare an array of pointers to functions returning bool * and initialize with A, B, and C */ bool (*farr[])() = {A, B, C}; ... /** * Call A, B, or C based on the value of i * (assumes i is in range of array) */ if (farr[i]()) // or (*farr[i])() { ... } ... } 
 //Declare the pointer and asign it to the function bool (*pFunc)() = A; //Call the function A pFunc(); //Call function B pFunc = B; pFunc(); //Call function C pFunc = C; pFunc(); 

我通常使用typedef来做它,但如果你不必经常使用函数指针,它可能有点过分。

 //assuming bool is available (where I come from it is an enum) typedef bool (*pmyfun_t)(); pmyfun_t pMyFun; pMyFun=A; //pMyFun=&A is actually same pMyFun(); 

这已经得到了充分的回答,但您可能会发现这很有用: 函数指针教程 。 这是一个真正全面的主题处理五章!

如果您需要有关复杂定义的帮助,例如:

 double (*(*pf)())[3][4]; 

看看我的左右规则。