David Refaeli David Refaeli - 1 year ago 90
C Question

Array of Function-Pointers with different functions return value (in C)

I've seen some examples of array of function-pointers (here, for example)

In the examples I've seen the array holds functions that have a return value of the same type (all int's, or all void's for example).

But I'm wondering can you have an array that holds function-pointers of different types?

The next code won't compile:

#include <stdio.h>

void Empty_Funcion()
int funcAdd(int a, int b){
return a+b;

int main()
int ret = 0;

void (*array[5])() = {&Empty_Funcion, &funcAdd, &Empty_Funcion, &funcAdd, &Empty_Funcion};

ret = (*array[1])(5,7);

printf("%d\n", ret);

return 0;

It says the problem is with the assignment ret =... "void value not ignored as it ought to be".

Answer Source

You can do it like this:

ret = ( ( int (*)(int,int) ) array[1] )(5,7);

You need to cast to pointer to function type.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download