简体   繁体   English

从函数返回函数指针并用指针调用它。 这是如何工作的?

[英]Returning a Function Pointer from a Function and Calling it with a Pointer. How does this Work Exactly?

So take the following code, I was reading some lecture notes on function pointers, and I came across this:所以拿下面的代码,我正在阅读一些关于函数指针的讲义,我遇到了这个:

int (*Convert(const char code))(int, int) {
    if (code == ‘+’) return ∑ // Takes two ints, and adds
    if (code == ‘-’) return &Difference; // Takes two ints, and subtracts
} 

int main () {
    int (*ptr)(int,int);
    ptr = Convert(‘+’);
    printf( “%d\n”, ptr(2,4));
} 

I'm usually used to seeing something like this when calling a function that returns a function pointer, and to me, this makes sense since I have all the parameters laid out here, the char , and the two int 's:在调用返回函数指针的函数时,我通常习惯于看到这样的事情,对我来说,这是有道理的,因为我在这里列出了所有参数, char和两个int

Convert('-')(5, 6);

But in the way it was written in the notes, I can't really grasp what's exactly going on here.但是按照笔记中写的方式,我无法真正理解这里到底发生了什么。 Can someone tell how exactly does this work?有人能告诉我这是如何工作的吗? Does it have to do with assigning (*ptr)(int, int) the function's address or something?它与分配(*ptr)(int, int)函数的地址有关吗?

Can someone tell how exactly does this work?有人能告诉我这是如何工作的吗? Does it have to do with assigning (*ptr)(int, int) the function's address or something?它与分配 (*ptr)(int, int) 函数的地址有关吗?

Function Convert() returns a pointer to a function -- either a pointer to Sum() or a pointer to Difference() , depending on its argument (or it terminates without specifying a return value, which is bad news for you if you do anything at all with the return value).函数Convert()返回一个指向函数的指针——要么是指向Sum()的指针,要么是指向Difference()的指针,这取决于它的参数(或者它在不指定返回值的情况下终止,如果你这样做,这对你来说是个坏消息任何带有返回值的东西)。 That function pointer is stored in variable ptr , which is declared to have the same type as Convert() returns.该函数指针存储在变量ptr ,该变量被声明为与Convert()返回的类型相同。 The pointed-to function can then be called by use of the function-call operator, () .然后可以使用函数调用运算符()调用指向的函数。

Perhaps it would be a bit clearer if rewritten in this equivalent way, with use of a typedef :如果使用typedef以这种等效方式重写,也许会更清楚一些:

typedef int (*op_function)(int, int);

op_function Convert(const char code) {
    if (code == ‘+’) return ∑ // Takes two ints, and adds
    if (code == ‘-’) return &Difference; // Takes two ints, and subtracts
} 

int main () {
    op_function ptr;
    ptr = Convert(‘+’);
    printf( “%d\n”, ptr(2,4));
}

Either there is a typo or you mean the function name AddSub instead of Convert .要么有拼写错误,要么是指函数名AddSub而不是Convert

For example例如

int (*AddSub(const char code))(int, int) {
    if (code == ‘+’) return ∑ // Takes two ints, and adds
    if (code == ‘-’) return &Difference; // Takes two ints, and subtracts
}

[Note: Pay attention to that using the operator & is redundant due to the implicit conversion of a function designator to pointer to the function as for example [注意:注意使用运算符&是多余的,因为函数指示符隐式转换为指向函数的指针,例如

int (*AddSub(const char code))(int, int) {
    if (code == ‘+’) return Sum; // Takes two ints, and adds
    if (code == ‘-’) return Difference; // Takes two ints, and subtracts
}

-end note.] -结束注释。]

So calling the function like所以调用函数就像

AddSub('-');

you will get an expression of the type pointer to function of the type int( int, int ).您将获得指向int( int, int ).类型函数的类型指针的表达式int( int, int ). And you can again supply arguments to the returned pointer that to call the pointed function like您可以再次为返回的指针提供参数以调用指向的函数,例如

AddSub('-')(5, 6);

To make it more clear you can rewrite the expression above like为了更清楚,你可以重写上面的表达式,如

( AddSub('-') )(5, 6);

It is the same as this code snippet它与此代码片段相同

int ( *ptr )( int, int ) = AddSub(‘+’);
printf( “%d\n”, ptr(2,4));

but without the intermediate variable ptr .但没有中间变量ptr

printf( “%d\n”, AddSub( '+' )(2,4) );

Pointers to functions keep reference to the function and can be called, but they behave exactly the same as other pointers.函数指针保持对函数的引用并且可以被调用,但它们的行为与其他指针完全相同。 The syntax is confusung many people and this is one of the places where hiding pointers behind the typedefs makes sense but it is not necessary语法让很多人感到困惑,这是在 typedef 后面隐藏指针有意义的地方之一,但没有必要

#include <stdio.h>
#include <stdlib.h>

typedef int func(int, int);

int Difference(int a, int b){
    return a - b;
}

int Sum(int a, int b){
    return a + b;
}

func *Convert(const char code) {
    if (code == '+') return Sum; // Takes two ints, and adds
    if (code == '-') return Difference; // Takes two ints, and subtracts
} 

int main () {
    func *ptr;
    ptr = Convert('+');
    printf( "%d\n", ptr(2,4));
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM