English 中文(简体)
• 如何通过作为参数的C类简单匿名功能
原标题:How to Pass Simple, Anonymous Functions as Parameters in C
  • 时间:2011-08-10 16:02:31
  •  标签:
  • c

我确信,这个问题有一些改动,但关于SO的所有其他类似问题似乎更为复杂,涉及穿透阵列和其他形式的数据。 我的设想非常简单,因此我希望有一个简单/可取的解决办法。

是否有办法可以创造匿名职能,还是通过一个密码线作为另一个职能点?

就我而言,我有一系列不同的行动。 在每项法典之前和之后,我想要完成的任务,这种任务永远不会改变。 我不想重复最初的法典和最后的法典,而是想写出一个功能点,作为参数,按必要的顺序执行所有法典。

我的问题是,这并不值得为每项行动确定30项职能,因为它们是每一条码。 如果我无法创造匿名职能,我是否能够简化我的《刑法》?

If my request isn t entirely clear. Here s a bit of pseudo-code for clarification. My code is much more meaningful than this but the code below gets the point accross.

void Tests()
{
  //Step #1
  printf("This is the beginning, always constant.");
  something_unique = a_var * 42;  //This is the line I d like to pass as an anon-function.
  printf("End code, never changes");
  a_var++;

  //Step #2
  printf("This is the beginning, always constant.");
  a_diff_var = "arbitrary";  //This is the line I d like to pass as an anon-function.
  printf("End code, never changes");
  a_var++;

  ...
  ...

  //Step #30
  printf("This is the beginning, always constant.");
  var_30 = "Yup, still executing the same code around a different operation.  Would be nice to refactor...";  //This is the line I d like to pass as an anon-function.
  printf("End code, never changes");
  a_var++;
}
最佳回答

不是在传统的匿名职能意义上,但你可以大体做到:

#define do_something(blah) {
    printf("This is the beginning, always constant.");
    blah;
    printf("End code, never changes");
    a_var++;
}

Then it becomes

do_something(something_unique = a_var * 42)
问题回答

您不能。 匿名功能只能用功能语言(和功能分立的语文)提供,我们大家都知道,c 功能失调;^

C和0xC++前,第2号。

在C++0x, 是的,使用

简化你的法典的最佳办法很可能是围绕开关单进行 lo。

int a_var;
for ( a_var = 0; a_var <= 30; a_var++ )
{
    starteroperations();
    switch (a_var)
    {
         case 0:
             operation0(); break;
         case ...:
             operationx(); break;
         case 30:
             ...
    }
    closingoperations();
 }

如果你能够使用地兰,你就可以利用区块。 为学习区块,可使用)提出的建议,即在标准C语文中增加部分,并增加博客员额的吨位。

您可使用区块撰写:

/* Block variables are declared like function pointers
 * but use ^ ("block pointer") instead of * ("normal pointer"). */
void (^before)(void) = void ^(void) { puts("before"); };

/* Blocks infer the return type, so you don t need to declare it
 * in the block definition. */
void (^after)(void) = ^(void) { puts("after"); };

/* The default arguments are assumed to be void, so you could even
 * just define after as
 *
 *     ^{ puts("after"); };
 */

before();
foo = bar + baz*kablooie;
after();

举例说,匿名区块名称被划入一个区块变量。 你们还可以直接界定并称之为一个障碍:

   ^{ puts("!"); } ();
/*|  definition   | invocation of anonymous function |*/

这还非常简单地界定了“结构目标”(在C中使用结构)。

Clang和海合会的支助 rel=“nofollow”>inner/nested functions,作为标准C的延伸。 这将使你在发言之前立即确定这一职能,如果你的控制流动结构允许,这可能是一种替代办法:不能允许内部职能点人员逃避其直接范围。 具体地说:

如果你在封面功能消失后试图通过地址宣布封顶功能,那么所有伤员都将停下空。 如果你试图在包含范围水平后将其称作已不复存在,如果它提到一些已不复存在的变数,那么你可能是 l的,但却不明智地承担风险。 但是,如果封顶的职能不提及任何已超出范围的事情,那么你就应当安全。

借助既定职能,你可以写:

 /* Nested functions are defined just like normal functions.
  * The difference is that they are not defined at "file scope"
  * but instead are defined inside another function. */
 void before(void) { puts("before"); };
 void after(void) { puts("after"); };

 before();
 foo = bar + baz*kablooie;
 after();

您要么通过@dcpomero所建议的case ,要么请你:

typedef void job(int);
job test1; void test1(int a_var) { something_unique = a_var * 42; }
job test2; void test2(int a_var) { a_diff_var = "arbitrary"; }
job test3; void test3(int a_var) { var_30 = "Yup, still executing the same code around a different operation.  Would be nice to refactor..."; }

job * tests[] = { test1, test2, test3, testn };

void Tests()
{
    int i;
    for (i=0; i < sizeof tests/sizeof tests[0]; i++) {
        printf("This is the beginning, always constant.");
        tests[i](a_var);
        printf("End code, never changes");
        a_var++;
    }
}




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签