How to define a pointer to function that returns a pointer to function?
typedef int(*a)(int,int);
a (*b)(int,int);
Why this can work,but the following can't work?
(int(*a)(int,int) ) (*b)(int,int);
or
int(*)(int,int) (*b)(int,int);
or
( int(*)(int,int) ) (*b)(int,int);
Here is the correct way to do it:
int (*(*b)(int,int))(int,int);
You can compile the following code that demonstrates using both of the 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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With