私は、異なる戻り値の型と異なる引数を持つ3つの関数を持っています。私は関数ポインタの配列を作成し、その後それらを呼び出すことを試みています。しかし、それは動作しません。提案をお願いします。異なる戻り値の型と引数を持つCの関数ポインタの配列
#include <stdio.h>
/* Array of function pointers (different return types and parameters) */
void sayHello()
{
printf("Hello World\n");
}
int add(int a, int b)
{
return a+b;
}
int twice(int a)
{
return 2*a;
}
int main()
{
int choice;
int(*add_ptr)(int,int) = NULL;
void(*hello_ptr)(void) = NULL;
int(*twice_ptr)(int) = NULL;
void * func_table[] = {(void *)sayHello, (void *)add, (void *)twice};
printf("Add : %d\n", ((add_ptr)func_table[1])(10,5));
printf("Hello : \n",((hello_ptr)func_table[0])());
printf("Twice : %d\n",((twice_ptr)func_table[2])(10));
return 0;
}
私が見えるようにコードを編集したEDIT
:
#include <stdio.h>
/* Array of function pointers (different return types and parameters) */
void sayHello()
{
printf("Hello World\n");
}
int add(int a, int b)
{
return a+b;
}
int twice(int a)
{
return 2*a;
}
int main()
{
int choice;
typedef int(*add_ptr)(int,int);
typedef void(*hello_ptr)(void);
typedef int(*twice_ptr)(int);
void * func_table[] = {(void *)sayHello, (void *)add, (void *)twice};
printf("Add : %d\n", ((add_ptr)func_table[1])(10,5));
printf("Hello : ",((hello_ptr)func_table[0])());
printf("Twice : %d\n",((twice_ptr)func_table[2])(10));
return 0;
}
しかし、それでもまだ、私はエラーを取得しています:
error: invalid use of void expression
printf("Hello : ",((hello_ptr)func_table[0])());
^
"_ただし、動作していません" - 何が機能していないかを説明してください。 –
_types_ではなく_variables_にキャストできます。 'int a; printf( "%d \ n"、(int)3.14); 'コンパイルして' 3'を出力しますが、 'int a; printf( "%d \ n"、(a)3.14); 'はできません。 –
http://ideone.com/GXveEC – BLUEPIXY