19

I am trying to really understand function pointers without using typedef but cannot seem to get this. I do not understand what signature is needed to convey that I return a pointer to a pointer to a function.

#include <stdio.h>

void odd()  { printf("odd!\n");  }
void even() { printf("even!\n"); }

void (*get_pfn(int i))()
{
    return i % 2 == 0 ? &even : &odd;
}

__SIGNATURE__
{
    return &get_pfn;
}

int main()
{
    get_pfn_pfn()(1)();
    get_pfn_pfn()(2)();
    return 0;
}

To make this work, what does __SIGNATURE__ have to be?

2

4 Answers 4

20

It has to return a function pointer to a function that takes an int and returns a function pointer:

void (*(*get_pfn_pfn(void))(int))(void) {
    return &get_pfn;
}

more lines:

void (*
        (*
             get_pfn_pfn(void)  // this is our function
        )(int i) // this is get_pfn(int)
     )(void)  // this is odd() or even()
{
    return &get_pfn;
}

The voids can be omitted, in which case the function pointer points to a function that takes unknown number of parameters. Which is not what you want. To declare a function pointer to a function that takes no arguments, you should add void inside function parameter list. The same way it's best to change get_pfn to void (*get_pfn(int i))(void). For example try calling from get_pfn(1)("some arg", "some other arg");. A C compiler will not give a warning, as empty () denote unknown arguments. To say that function takes no arguments, you have to (void).

For many the sequences of braces, especially ))(, in function pointers are hard to parse. That's why many prefer typedefs for function pointers or types:

typedef void get_pfn_func_t(void);    
get_pfn_func_t *get_pfn(int i) {
    return i % 2 == 0 ? &even : &odd;
}

typedef get_pfn_func_t *get_pfn_pfn_func_t(int i);
get_pfn_pfn_func_t *get_pfn_pfn(void) {
    return &get_pfn;
}
3
  • 1
    Thank you so so much Kamil!! I like that broken down/visualized tree like view, it helped make it a lot more understandable :) !!
    – Jas
    Sep 10, 2019 at 17:23
  • 1
    @Jas You may like it but I don't think it is recommended to write code in this manner.
    – machine_1
    Sep 10, 2019 at 17:25
  • 1
    I wish there was a website that had a GUI representation of a function pointer, let you move various components around, etc. Something like nsdateformatter.com
    – Alexander
    Sep 11, 2019 at 1:55
7

Function pointers without a typedef can be tricky to work with. To figure them out, you work from the inside out.

So let's break down exactly how we come up with the correct function signature.

get_pfn_pfn is a function:

get_pfn_pfn()

Which takes no parameters:

get_pfn_pfn(void)

And returns a pointer:

*get_pfn_pfn(void)

To a function:

(*get_pfn_pfn(void))()

Which takes an int parameter:

(*get_pfn_pfn(void))(int)

And returns a pointer:

*(*get_pfn_pfn(void))(int)

To a function:

(*(*get_pfn_pfn(void))(int))()

Which takes no parameters:

(*(*get_pfn_pfn(void))(int))(void)

And returns nothing (i.e. void):

void (*(*get_pfn_pfn(void))(int))(void)

Of course, using typedef's simplifies this greatly.

First the type for even and odd:

typedef void (*func1)(void);

Which we can then apply to get_pfn:

func1 get_pfn(int) { ... }

Then the type for this function:

typedef func1 (*func2)(int);

Which we can apply to get_pfn_pfn:

func2 get_pfn_pfn(void) { ... }
7

The return type of the function get_pfn is -

void (*) ();

So type of &get_pfn is -

void (*(*)(int))()

Now, this function returns this type, hence its signature will be -

void (*(*(foo)())(int))()

You can verify this by typing this in cdecl.org

2
  • Thank you very much Ajay! Your explanation breaking down the steps helped me understand it more easily, but unfortunately the signature did not work sorry :(
    – Jas
    Sep 10, 2019 at 17:22
  • 1
    @Jas, sorry I messed up the order of the (int). I thought your odd even functions take a param int. I will correct it. Sep 10, 2019 at 17:23
1

it's this way:

void (*(*get_pfn_pfn(void))(int))()
1
  • 2
    The other answers explain it in a better way.
    – glglgl
    Sep 11, 2019 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.