C 条件函数定义
我有一个很长的while循环代码。在这个while循环中,有一个很长的switch case函数,以便知道应该在C 条件函数定义,c,function,function-pointers,C,Function,Function Pointers,我有一个很长的while循环代码。在这个while循环中,有一个很长的switch case函数,以便知道应该在条上应用哪个函数 int i=0; while(i<BigNumber) { switch(variable) { case 1: foo1(bar); case 2: foo2(bar); etc... } i = i+1; } 但不能在main中定义函数,因此该代码无法编译。
条上应用哪个函数
int i=0;
while(i<BigNumber)
{
switch(variable)
{
case 1:
foo1(bar);
case 2:
foo2(bar);
etc...
}
i = i+1;
}
但不能在main中定义函数,因此该代码无法编译。我如何解决这个问题?有没有办法定义一个有条件的函数?最好的解决方案是创建一个指向函数的指针数组,然后通过执行类似于(*myarray[variable])(bar)
?的操作在while循环中调用正确的函数,以详细说明happydave的注释吗
void (*foo)(int);
switch(variable)
{
case 1:
foo = foo1; // or foo = &foo1; // & is optional
case 2:
foo = foo2;
etc...
}
int i=0;
while(i<BigNumber)
{
foo(bar); // (*foo)(bar); also works
i = i+1;
}
void(*foo)(int);
开关(可变)
{
案例1:
foo=foo1;//或foo=&foo1;//&是可选的
案例2:
foo=foo2;
等
}
int i=0;
而(i这里,函数指针是为解决这类问题而定制的。您将定义正常函数foo1
、foo2
,以及您可能需要的任何其他函数。然后使用语法返回类型(*fpointername)(arg,list)定义函数指针
您的arg
,列表
参数就是要分配的函数的类型(例如,如果您的函数是void foo1(int a,char b)
,那么您将函数指针声明为void(*fpointername)(int,char);
(fn指针定义中省略了变量名)
下面是一个简单的程序,说明了这一点。如果有任何问题,请发表评论:
#include <stdio.h>
/* 3 void functions, one of which will become foo
* dependeing on the outcome of the switch statement
*/
void foo1 (char c) {
printf ("\n %s(%c)\n\n", __func__, c);
}
void foo2 (char c) {
printf ("\n %s(%c)\n\n", __func__, c);
}
void foodefault (char c) {
printf ("\n %s(%c)\n\n", __func__, c);
printf (" Usage: program char [a->foo1, b->foo2, other->foodefault]\n\n");
}
/* simple program passes argument to switch
* statement which assigns function to function
* pointer 'foo ()' based on switch criteria.
*/
int main (int argc, char **argv) {
void (*foo)(char) = NULL; /* function pointer initialized to NULL */
char x = (argc > 1) ? *argv[1] : 'c'; /* set 'x' value depending on argv[1] */
switch (x) /* switch on input to determine foo() */
{
case 'a' : /* input 'a' */
foo = &foo1; /* assign foo as foo1 */
break;
case 'b' : /* input 'b' */
foo = &foo2; /* assign foo as foo2 */
break;
default : /* default case assign foo foodefault */
foo = &foodefault;
}
foo (x); /* call function foo(x) */
return 0;
}
没有太大的不同,但是您可以只创建一个指向函数变量的指针,并在while循环之外的switch语句中分配它。无需创建指针数组。
#include <stdio.h>
/* 3 void functions, one of which will become foo
* dependeing on the outcome of the switch statement
*/
void foo1 (char c) {
printf ("\n %s(%c)\n\n", __func__, c);
}
void foo2 (char c) {
printf ("\n %s(%c)\n\n", __func__, c);
}
void foodefault (char c) {
printf ("\n %s(%c)\n\n", __func__, c);
printf (" Usage: program char [a->foo1, b->foo2, other->foodefault]\n\n");
}
/* simple program passes argument to switch
* statement which assigns function to function
* pointer 'foo ()' based on switch criteria.
*/
int main (int argc, char **argv) {
void (*foo)(char) = NULL; /* function pointer initialized to NULL */
char x = (argc > 1) ? *argv[1] : 'c'; /* set 'x' value depending on argv[1] */
switch (x) /* switch on input to determine foo() */
{
case 'a' : /* input 'a' */
foo = &foo1; /* assign foo as foo1 */
break;
case 'b' : /* input 'b' */
foo = &foo2; /* assign foo as foo2 */
break;
default : /* default case assign foo foodefault */
foo = &foodefault;
}
foo (x); /* call function foo(x) */
return 0;
}
$ ./bin/foofn
foodefault(c)
Usage: program char [a->foo1, b->foo2, other->foodefault]
$ ./bin/foofn a
foo1(a)
$ ./bin/foofn b
foo2(b)
$ ./bin/foofn d
foodefault(d)
Usage: program char [a->foo1, b->foo2, other->foodefault]