如何声明一个返回函数指针的函数?

发布于 2024-12-13 23:53:15 字数 190 浏览 2 评论 0原文

想象一个带有参数 double 和 int 的函数 myFunctionA:

myFunctionA (double, int);

该函数应该返回一个函数指针:

char (*myPointer)();

如何在 C 中声明该函数?

Imagine a function myFunctionA with the parameter double and int:

myFunctionA (double, int);

This function should return a function pointer:

char (*myPointer)();

How do I declare this function in C?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温柔一刀 2024-12-20 23:53:15

typedef 是你的朋友:

typedef char (*func_ptr_type)();
func_ptr_type myFunction( double, int );

typedef is your friend:

typedef char (*func_ptr_type)();
func_ptr_type myFunction( double, int );
停顿的约定 2024-12-20 23:53:15
void (*fun(double, int))();

根据 right-left-rulefun 是一个 double, int 的函数,返回一个指向参数不确定的函数的指针,返回 void

编辑:这是该规则的另一个链接。

编辑2:这个版本只是为了紧凑并表明它确实可以完成。

在这里使用 typedef 确实很有用。但不是指向指针,而是指向函数类型本身

为什么?因为可以将它用作一种原型,从而确保功能确实匹配。并且因为作为指针的身份仍然可见。

所以一个好的解决方案是

typedef char specialfunc();
specialfunc * myFunction( double, int );

specialfunc specfunc1; // this ensures that the next function remains untampered
char specfunc1() {
    return 'A';
}

specialfunc specfunc2; // this ensures that the next function remains untampered
// here I obediently changed char to int -> compiler throws error thanks to the line above.
int specfunc2() {
    return 'B';
}

specialfunc * myFunction( double value, int threshold)
{
    if (value > threshold) {
        return specfunc1;
    } else {
        return specfunc2;
    }
}
void (*fun(double, int))();

According to the right-left-rule, fun is a function of double, int returning a pointer to a function with uncertain parameters returning void.

EDIT: This is another link to that rule.

EDIT 2: This version is only for the sake of compactness and for showing that it really can be done.

It is indeed useful to use a typedef here. But not to the pointer, but to the function type itself.

Why? Because it is possible to use it as a kind of prototype then and so ensure that the functions do really match. And because the identity as a pointer remains visible.

So a good solution would be

typedef char specialfunc();
specialfunc * myFunction( double, int );

specialfunc specfunc1; // this ensures that the next function remains untampered
char specfunc1() {
    return 'A';
}

specialfunc specfunc2; // this ensures that the next function remains untampered
// here I obediently changed char to int -> compiler throws error thanks to the line above.
int specfunc2() {
    return 'B';
}

specialfunc * myFunction( double value, int threshold)
{
    if (value > threshold) {
        return specfunc1;
    } else {
        return specfunc2;
    }
}
淡莣 2024-12-20 23:53:15

制作一个类型定义:

typedef int (*intfunc)(void);

int hello(void)
{
    return 1;
}

intfunc hello_generator(void)
{
    return hello;
}

int main(void)
{
    intfunc h = hello_generator();
    return h();
}

Make a typedef:

typedef int (*intfunc)(void);

int hello(void)
{
    return 1;
}

intfunc hello_generator(void)
{
    return hello;
}

int main(void)
{
    intfunc h = hello_generator();
    return h();
}
遇见了你 2024-12-20 23:53:15
char * func() { return 's'; }

typedef char(*myPointer)();
myPointer myFunctionA (double, int){ /*Implementation*/ return &func; }
char * func() { return 's'; }

typedef char(*myPointer)();
myPointer myFunctionA (double, int){ /*Implementation*/ return &func; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文