How to define a pointer to a function that returns a pointer to a function

How to define a pointer to a function that returns a pointer to a function?

typedef int(*a)(int,int); a (*b)(int,int); 

Why can this work, but the following cannot work?

 (int(*a)(int,int) ) (*b)(int,int); 

or

 int(*)(int,int) (*b)(int,int); 

or

 ( int(*)(int,int) ) (*b)(int,int); 
+4
source share
1 answer

Here is the right way to do this:

 int (*(*b)(int,int))(int,int); 

You can compile the following code demonstrating the use of both methods. I would personally use the typedef method for clarity.

 #include <stdio.h> int addition(int x,int y) { return x + y; } int (*test(int x, int y))(int,int) { return &addition; } typedef int (*a)(int, int); int main() { a (*b)(int,int); int (*(*c)(int,int))(int,int); b = &test; c = &test; printf(b == c ? "They are equal!\n" : "They are not equal :(\n"); } 
+3
source

Source: https://habr.com/ru/post/1398340/


All Articles