【问题标题】:Conditional Function definition条件函数定义
【发布时间】:2014-10-27 02:56:59
【问题描述】:

我有一个很长的 while 循环代码。在这个 while 循环中,有一个很长的 switch-case 函数,以便知道应该在 bar 上应用哪个函数。

int i=0;
while(i<BigNumber)
{
   switch(variable)
   {
      case 1:
          foo1(bar);
      case 2:
          foo2(bar);
      etc...
   }
   i = i+1;
}

但是,从一次迭代到另一次 while 循环,switch-case 结构中的情况总是相同的。因此,我认为在 while 循环之前定义一个独特的函数 foo 以应用于 bar 会更智能,但我可以对 foo 进行这个条件定义吗?类似的东西

switch(variable)
{
   case 1:
      define foo this way
   case 2:
      define foo that way
   etc...
}

int i=0;
while(i<BigNumber)
{
   foo(bar);
   i = i+1;
}

但是不能在 main 中定义函数,所以这段代码编译失败。我该如何解决这个问题?有什么方法可以定义一个有条件的函数吗?最好的解决方案是否包括创建一个指向函数的指针数组,然后通过执行(*myarray[variable])(bar) 之类的操作在 while 循环中调用正确的函数?

【问题讨论】:

  • 差别不大,但您可以只创建一个指向函数的变量,并在您的 while 循环之外的 switch 语句中分配它。无需创建指针数组。

标签: c function function-pointers


【解决方案1】:

在这里,函数指针是为解决此类挑战而量身定制的。您将定义您的正常功能foo1foo2,以及您可能需要的更多功能。然后,您使用语法return type (*fpointername)(arg, list); 定义function pointer 您的arglist 参数只是要分配的函数的type。 (例如,如果您的函数是 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;
}

输出:

$ ./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]

【讨论】:

    【解决方案2】:

    详细说明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;
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多