The Programming M16A4
The Programming M16A4

Reputation: 397

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:

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?

Upvotes: 0

Views: 472

Answers (3)

0___________
0___________

Reputation: 67546

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

#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));
} 

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 310990

Either there is a typo or you mean the function name AddSub instead of Convert.

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
}

[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 ). 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.

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

Upvotes: 1

John Bollinger
John Bollinger

Reputation: 180236

Can someone tell how exactly does this work? Does it have to do with assigning (*ptr)(int, int) the function's address or something?

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). That function pointer is stored in variable ptr, which is declared to have the same type as Convert() returns. 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 int (*op_function)(int, int);

op_function Convert(const char code) {
    if (code == ‘+’) return &Sum; // 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));
}

Upvotes: 2

Related Questions