Syntax for a function pointer that returns a function pointer in C

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

Also, in what scenario will a function pointer be used that returns a function pointer?

+5
source share
3 answers

This is trivial with typedefs:

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

FP1is a type of function pointer that returns a pointer to a type function FP0.

As for when this is useful, good, useful if you have a function that returns a pointer to a function, and you need to get or save a pointer to this function.

+9
source

typedef, . , signal() C:

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

void handler(int signum)
{
    ...
}

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

typedefs, :

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

void handler(int signum)
{
    ...
}

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

, signal() <signal.h>, .

+5

If you do not want a second typedef,

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

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

+1
source

All Articles