More of a matter of curiosity than anything. Basically I want to know if it's possible to declare multiple function pointers in a line, something like:
int a = 1, b = 2;
With function pointers? Without having to resort to typedef
.
I've tried void (*foo = NULL, *bar = NULL)(int)
. Unsurprisingly, this didn't work.
Try as follows:
void (*a)(int), (*b)(int);
void test(int n)
{
printf("%d\n", n);
}
int main()
{
a = NULL;
a = test;
a(1);
b = test;
b(2);
return 0;
}
EDIT:
Another form is array of function pointers:
void (*fun[2])(int) = {NULL, NULL};
void test(int n)
{
printf("%d\n",n);
}
int main()
{
fun[0] = NULL;
fun[0] = test;
fun[0](1);
fun[1] = test;
fun[1](2);
}
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