home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / FPSWITCH.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  1KB  |  84 lines

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