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

  1.  /* Demonstrates passing a pointer to a multidimensional */
  2.  /* array to a function. */
  3.  
  4.  #include <stdio.h>
  5.  
  6.  void printarray_1(int (*ptr)[4]);
  7.  void printarray_2(int (*ptr)[4], int n);
  8.  
  9.  main()
  10. {
  11.      int  multi[3][4] = { { 1, 2, 3, 4 },
  12.                          { 5, 6, 7, 8 },
  13.                          { 9, 10, 11, 12 } };
  14.     /* ptr is a pointer to an array of 4 ints. */
  15.  
  16.       int (*ptr)[4], count;
  17.  
  18.      /* Set ptr to point to the first element of multi. */
  19.  
  20.      ptr = multi;
  21.  
  22.      /* With each loop, ptr is incremented to point at the next */
  23.      /* element (that is, next 4-element integer array) of multi. */
  24.  
  25.      for (count = 0; count < 3; count++)
  26.           printarray_1(ptr++);
  27.  
  28.      puts("\n\nPress a key...");
  29.      getch();
  30.  
  31.      printarray_2(multi, 3);
  32.  
  33.  }
  34.  
  35.   void printarray_1(int (*ptr)[4])
  36.  {
  37.      /* Prints the elements of a single four-element integer array. */
  38.      /* p is a pointer to type int. You must use a type cast */
  39.      /* to make p equal to the address in ptr. */
  40.  
  41.      int *p, count;
  42.      p = (int *)ptr;
  43.  
  44.      for (count = 0; count < 4; count++)
  45.          printf("\n%d", *p++);
  46.  }
  47.  
  48.  void printarray_2(int (*ptr)[4], int n)
  49.  {
  50.      /* Prints the elements of an n by four-element integer array. */
  51.  
  52.      int *p, count;
  53.      p = (int *)ptr;
  54.  
  55.      for (count = 0; count < (4 * n); count++)
  56.          printf("\n%d", *p++);
  57.  }
  58.