call function with function pointer array as argument

F

Felix Kater

Hi,

I've got a function with an array of 12 function pointers as an argument. When calling it: Is there a way to directly insert and fill the array argument (using braces {} or whatever) -- or do I need to fill an array first and then pass it as an argument?

Felix
 
B

Bill Pursell

Felix said:
Hi,

I've got a function with an array of 12 function pointers as an argument. When calling it: Is there a way to directly insert and fill the array argument (using braces {} or whatever) -- or do I need to fill an array first and then pass it as an argument?

The following is not ISO-C (gcc generates the warning:
"a.c:20: warning: ISO C90 forbids compound literals"),
but it might be what you're looking for:

int foo0(void) {return 0;}
int foo1(void) {return 1;}
int foo2(void) {return 2;}
int foo3(void) {return 3;}

void FOO( int (**f)(void), int i)
{
for (; i>=0; i--)
printf("%d\n", f());
}

typedef int (*f_array[4])(void);

int main(void)
{
f_array f;

FOO( (f_array){foo0, foo1, foo2, foo3}, 3);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top