home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tyc / list15_9.c < prev    next >
C/C++ Source or Header  |  1993-10-16  |  849b  |  58 lines

  1.  /* Using a pointer to call different functions. */
  2.  
  3.  #include <stdio.h>
  4.  
  5. /* The function prototypes. */
  6.  
  7.  void func1(int x);
  8.  void one(void);
  9.   void two(void);
  10.  void other(void);
  11.  
  12.   main()
  13.  {
  14.       int a;
  15.  
  16.      for (;;)
  17.      {
  18.          puts("\nEnter an integer between 1 and 10, 0 to exit: ");
  19.           scanf("%d", &a);
  20.  
  21.           if (a == 0)
  22.              break;
  23.  
  24.          func1(a);
  25.      }
  26.  }
  27.  
  28.  void func1(int x)
  29.  {
  30.      /* The pointer to function. */
  31.  
  32.      void (*ptr)(void);
  33.  
  34.      if (x == 1)
  35.          ptr = one;
  36.      else if (x == 2)
  37.          ptr = two;
  38.      else
  39.          ptr = other;
  40.  
  41.      ptr();
  42.  }
  43.  
  44.  void one(void)
  45.  {
  46.      puts(" You entered 1.");
  47.  }
  48.  
  49.  void two(void)
  50.  {
  51.      puts("You entered 2.");
  52.  }
  53.  
  54.  void other(void)
  55.  {
  56.      puts("You entered something other than 1 or 2.");
  57.  }
  58.