c - 一种奇怪的函数定义方式

标签 c function user-defined-functions

我学了一年的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/

相关文章:

c - 在C中将字符串存储在数组中

c - 杀死一个 fork 的 child

Javascript 哈希器嵌套函数

firebird - 使用 c++builder 2010 为 firebird 2.5 创建用户定义函数

excel - VBA中CDate函数的问题

mysql - 在centos 6云服务器上安装Levenstein Functions

c - 在 Hp-UX ksh 中执行交互式命令(意外)并获取子进程

Python 递归限制解决方法

javascript - 能够在渲染 Highcharts 后显示检查系列的标签

C (Linux) 检查套接字