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

  1.  /* Using type void pointers. */
  2.  
  3.  #include <stdio.h>
  4.  
  5.  void half(void *x, char type);
  6.  
  7.  main()
  8.  {
  9.      /* Initialize one variable of each type. */
  10.  
  11.      int i = 20;
  12.      long l = 100000;
  13.      float f = 12.456;
  14.      double d = 123.044444;
  15.  
  16.      /* Display their initial values. */
  17.  
  18.      printf("\n%d", i);
  19.      printf("\n%ld", l);
  20.      printf("\n%f", f);
  21.      printf("\n%lf\n\n", d);
  22.  
  23.      /* Call half() for each variable. */
  24.  
  25.      half(&i, 'i');
  26.      half(&l, 'l');
  27.      half(&d, 'd');
  28.      half(&f, 'f');
  29.  
  30.      /* Display their new values. */
  31.  
  32.      printf("\n%d", i);
  33.      printf("\n%ld", l);
  34.      printf("\n%f", f);
  35.      printf("\n%lf", d);
  36.  }
  37.  
  38.  void half(void *x, char type)
  39.  {
  40.      /* Depending on the value of type, cast the */
  41.      /* pointer x appropriately and divide by 2. */
  42.  
  43.      switch (type)
  44.      {
  45.          case 'i':
  46.              {
  47.              *((int *)x) /= 2;
  48.              break;
  49.              }
  50.          case 'l':
  51.              {
  52.              *((long *)x) /= 2;
  53.              break;
  54.              }
  55.          case 'f':
  56.              {
  57.              *((float *)x) /= 2;
  58.              break;
  59.              }
  60.          case 'd':
  61.              {
  62.              *((double *)x) /= 2;
  63.              break;
  64.              }
  65.      }
  66.  }
  67.