Advertisement
Wojtekd

Array of Function Pointers

May 11th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef void (*VoidVoid)( void );
  5. typedef void (*VoidInt)( int );
  6. typedef int (*IntInt)( int );
  7.  
  8. void func1( void );
  9. void func2( int );
  10. int func3( int );
  11.  
  12. int main(void)
  13. {
  14.     VoidVoid tab[3] = { func1, (VoidVoid)func2, (VoidVoid)func3 };
  15.  
  16.     int choice = 1;
  17.     switch(choice)
  18.     {
  19.         case 0:
  20.         {
  21.             VoidVoid func;
  22.             func = tab[choice];
  23.             func();
  24.             // lub tab[choice]();          
  25.             break;
  26.         }
  27.         case 1:
  28.         {          
  29.             VoidInt func;
  30.             func = (VoidInt)tab[choice];
  31.             func(5);
  32.             break;
  33.         }
  34.         case 2:
  35.         {
  36.             IntInt func;
  37.             func = (IntInt)tab[choice];
  38.             func(5);
  39.            
  40.             break;
  41.         }
  42.         default:
  43.         {
  44.             break;
  45.         }
  46.     }
  47.     return 0;  
  48. }
  49. void func1( void )
  50. {
  51.     puts("func1");
  52. }
  53. void func2( int a )
  54. {
  55.     puts("func2");
  56. }
  57. int func3( int b)
  58. {
  59.     puts("func3");
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement