home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNPD9404.ZIP / FPSWITCH.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  1KB  |  82 lines

  1. /*
  2. **  FPSELECT.C - Demonstrates using function pointers in lieu of switches
  3. */
  4.  
  5. #include <stdlib.h>     /* for NULL */
  6.  
  7. /*  Declare your functions here     */
  8.  
  9. char *cpfunc1(int);
  10. char *cpfunc2(int);
  11. char *cpfunc3(int);
  12.  
  13. void vfunc1(void);
  14. void vfunc2(void);
  15. void vfunc3(void);
  16. void vfunc4(void);
  17.  
  18. /*
  19. **  Old ways using switch statements
  20. */
  21.  
  22. char *oldcpswitch(int select, int arg)
  23. {
  24.       switch (select)
  25.       {
  26.       case 1:
  27.             return cpfunc1(arg);
  28.       
  29.       case 2:
  30.             return cpfunc2(arg);
  31.       
  32.       case 3:
  33.             return cpfunc3(arg);
  34.             
  35.       default:
  36.             return NULL;
  37.       }
  38. }
  39.  
  40. void oldvswitch(int select)
  41. {
  42.       switch (select)
  43.       {
  44.       case 1:
  45.             vfunc1();
  46.             break;
  47.       
  48.       case 2:
  49.             vfunc2();
  50.             break;
  51.       
  52.       case 3:
  53.             vfunc3();
  54.             break;
  55.       
  56.       case 4:
  57.             vfunc4();
  58.             break;
  59.       }
  60. }
  61.  
  62. /*
  63. **  Using function pointers
  64. */
  65.  
  66. char *newcpswitch(int select, int arg)
  67. {
  68.       char *(*cpfunc[3])(int) = { cpfunc1, cpfunc2, cpfunc3 };
  69.  
  70.       if (select < 1 || select > 3)
  71.             return NULL;
  72.       return (*cpfunc[select-1])(arg);
  73. }
  74.  
  75. void newvswitch(int select)
  76. {
  77.       void (*vfunc[4])(void) = { vfunc1, vfunc2, vfunc3, vfunc4 };
  78.  
  79.       if (select > 0 && select < 5)
  80.             (*vfunc[select-1])();
  81. }
  82.