fork download
  1. #include <stdio.h>
  2.  
  3. /* Array of function pointers (different return types and parameters) */
  4.  
  5. void sayHello()
  6. {
  7. printf("Hello World\n");
  8. }
  9.  
  10. int add(int a, int b)
  11. {
  12. return a+b;
  13. }
  14.  
  15. int twice(int a)
  16. {
  17. return 2*a;
  18. }
  19.  
  20. int main()
  21. {
  22. int choice;
  23. typedef int(*add_ptr)(int,int);
  24. typedef void(*hello_ptr)(void);
  25. typedef int(*twice_ptr)(int);
  26.  
  27. void * func_table[] = {(void *)sayHello, (void *)add, (void *)twice};
  28.  
  29.  
  30. printf("Add : %d\n", ((add_ptr)func_table[1])(10,5));
  31. ((hello_ptr)func_table[0])();
  32. printf("Twice : %d\n",((twice_ptr)func_table[2])(10));
  33. return 0;
  34. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function 'main':
prog.c:27:28: error: ISO C forbids conversion of function pointer to object pointer type [-Werror=pedantic]
     void * func_table[] = {(void *)sayHello, (void *)add, (void *)twice};
                            ^
prog.c:27:46: error: ISO C forbids conversion of function pointer to object pointer type [-Werror=pedantic]
     void * func_table[] = {(void *)sayHello, (void *)add, (void *)twice};
                                              ^
prog.c:27:59: error: ISO C forbids conversion of function pointer to object pointer type [-Werror=pedantic]
     void * func_table[] = {(void *)sayHello, (void *)add, (void *)twice};
                                                           ^
prog.c:30:27: error: ISO C forbids conversion of object pointer to function pointer type [-Werror=pedantic]
     printf("Add : %d\n", ((add_ptr)func_table[1])(10,5));
                           ^
prog.c:31:6: error: ISO C forbids conversion of object pointer to function pointer type [-Werror=pedantic]
     ((hello_ptr)func_table[0])();
      ^
prog.c:32:28: error: ISO C forbids conversion of object pointer to function pointer type [-Werror=pedantic]
     printf("Twice : %d\n",((twice_ptr)func_table[2])(10));
                            ^
prog.c:22:9: error: unused variable 'choice' [-Werror=unused-variable]
     int choice;
         ^
cc1: all warnings being treated as errors
stdout
Standard output is empty