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?
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?
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.
template <...> auto foo() -> int(*)(void) would work just fineIf 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.
* 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.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))