6

How to declare a pointer to a function returning another function pointer? Please share with me the syntax and an example code snippet.

Also, in which scenario would a function pointer returning a function pointer be used?

2
  • 3
    Please don't add your name at the end of the posts as your signature will appear at the end of the posts automatically! stackoverflow.com/faq#signatures Commented Jan 4, 2012 at 6:25
  • I've tweaked you title and text in an effort to make them more comprehensible. You are, of course, free to switch it back if you don't like the changes, but I would suggest that you consider the difference carefully as it might clarify the problem a little. Commented Jan 5, 2012 at 1:17

3 Answers 3

9

This is trivial with typedefs:

typedef int(*FP0)(void);
typedef FP0(*FP1)(void);

FP1 is the type of a pointer to a function that returns a function pointer of type FP0.

As for when this is useful, well, it is useful if you have a function that returns a function pointer and you need to obtain or store a pointer to this function.

Sign up to request clarification or add additional context in comments.

3 Comments

declaration is fine, but can you give me a code snippet which uses function pointer and returns a funtion pointer. that would be help ful for me to understand clearly. Also, give me the syntax with and with out typedef..
Typedefs are not always viable. I.e. when you don't have a compiler that does template aliases, and you want to return a function pointer from a function template, and the returned signature depends on a template argument. (You can 'work around it' by using an 'identity type trait')
PS. Or of course, C++11 late return types :) template <...> auto foo() -> int(*)(void) would work just fine
6

If you avoid using typedef, it is hard. For example, consider signal() from the C standard:

extern void (*signal(int, void (*)(int)))(int);

void handler(int signum)
{
    ...
}

if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    signal(SIGINT, handler);

Using typedefs, it is easier:

typedef void Handler(int);
extern Handler *signal(int, Handler *);

void handler(int signum)
{
    ...
}

if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    signal(SIGINT, handler);

Note that for the signal() function, you would normally simply use <signal.h> and let the system worry about declaring it.

4 Comments

In your typedef example, shouldn't Handler be defined using (*Handler), and then signal as extern Handler signal(int, Handler)? Function pointer syntax in C is a mess, IMHO, it has two distinct syntaxes that work exactly the same.
@Spidey: what you suggest is the more typical way to do it, probably, but that which is shown works correctly too. The advantage is that you can see the pointer in the declarations. Both work.
Cool. What troubles me most is that (main == &main) is true. It's probably like this for compatibility with older code base, but if I were to define a code standard, that would be the first point that I'd be sure to make explicit which notation to be used.
@Spidey: That's a slightly different topic. Function pointers are quite unlike pointers to data, and you can use multiple * to dereference them (with the same effect as one *), and you can take the address of a function name and end up with the same value (which is what your main == &main shows). Given: void (*func)(void); appropriately initialized, you can invoke the function with any of: func(); (*func)(); (**func)(); (***func)();, etc. The C99 Rationale for §6.5.2.2 explicitly shows similar notations.
1

If you don't want a second typedef,

typedef float(*(*fptr_t)(int))(double)

this means "declare fptr_t as pointer to function (int) returning pointer to function (double) returning float" (fptr_t : int → (double → float))

1 Comment

Declaration looks fine, but not sure about assignment and invoking the funciton using this pointer. Is it same as normal function pointer invoking? Also, pls give me a code snippet which makes me to underst very clearly.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.