Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you declare multiple function pointers in a single line without typedeffing?

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.

like image 715
R.D. Avatar asked Aug 24 '13 04:08

R.D.


1 Answers

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);
}
like image 135
vvy Avatar answered Oct 24 '22 04:10

vvy