How to declare a function pointer that returns a function pointer

  • A+
Category:Languages

How do you declare a function pointer that points to a function that has the same parameters and also returns a pointer to a function with the same parameters.

i.e. funcPtr points to func1(int a, int b), and func1 returns a pointer to another function func2(int a, int b). func2 also returns a function pointer with the same signature as func1.

TYPE funcPtr = func1; funcPtr = funcPtr(0, 1); 

How does one declare funcPtr? What should TYPE be?

 


This is not possible directly. You can instead declare that the function return a structure, and the structure can contain a pointer to a function.

struct func {     struct func (*func) (void); };  struct func foo (void); struct func bar (void);  struct func foo (void) { return (struct func){ bar }; } struct func bar (void) { return (struct func){ foo }; }  ...     struct func funcPtr = { foo };     funcPtr = funcPtr.func(); 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: