我学了一年的C,我看到了一种定义函数的类型。我不能给它命名,所以我想知道定义函数的名称。
我们将函数定义为标准:
FunctionReturnType FunctionName(FunctionArgs)
{
codes();
}
但是我看到了那个类型:
FunctionReturnType FunctionName(FunctionArgs)(TheWeirdArea)
{
codes();
}
那么我们在名为 TheWeirdArea 的地方做什么?我想这与函数参数有关,但我想正确地知道我们在 TheWeirdArea 中做了什么。
最佳答案
我想这是关于返回一个函数指针到一个看起来像这样的函数
FunctionReturnType func(TheWeirdArea);
这是一个例子:
#include <stdio.h>
int bar1(int b)
{
printf("bar1\n");
return 42 + b;
}
int bar2(int b)
{
printf("bar2\n");
return 100 + b;
}
int (*foo(int a))(int b) // Returns a function pointer
{
if (a == 1) return bar1;
return bar2;
}
int main(void) {
printf("%d\n", foo(1)(100));
printf("%d\n", foo(2)(200));
return 0;
}
输出:
bar1
142
bar2
300
关于c - 一种奇怪的函数定义方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49571912/