home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch9 / ptrptr.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  2KB  |  61 lines

  1. /*                      ptrptr.c
  2.  *
  3.  *   Synopsis  - Outputs information, strings, and individual
  4.  *               characters in an array of pointers to type char.
  5.  *
  6.  *   Objective - To illustrate double indirection and use the
  7.  *               pointer to a pointer to traverse an array of
  8.  *               pointers.
  9.  */
  10.  
  11. /* Include Files */
  12. #include <stdio.h>
  13. #include <string.h>
  14.  
  15. int main( void )
  16. {
  17.      char *ptrarray[] = { "George",
  18.                           "Elliot's",
  19.                           "Oldest",
  20.                           "Girl",
  21.                           "Rode",
  22.                           "A",
  23.                           "Pig",
  24.                           "Home",
  25.                           "Yesterday",
  26.                           ""                             /* Note 1 */
  27.                         };
  28.  
  29.      char **ptrptr = ptrarray;                           /* Note 2 */
  30.  
  31.                                                          /* Note 3 */
  32.      printf( "sizeof( ptrarray )  %d,\tsizeof( ptrptr )  %d\n",
  33.                                   sizeof( ptrarray ), sizeof( ptrptr ));
  34.      printf( "ptrarray          %p,\tptrptr         %p\n",
  35.                                   ptrarray, ptrptr );
  36.      printf( "ptrarray[0]       %p,\t*ptrptr        %p\n\n",
  37.                                   ptrarray[0], *ptrptr );
  38.  
  39.                                                          /* Note 4 */
  40.      printf( "ptrarray[0]   %s,\t*ptrptr       %s\n",
  41.                                   ptrarray[0], *ptrptr );
  42.      printf( "ptrarray[1]   %s,\t*( ptrptr+1 ) %s\n\n",
  43.                                   ptrarray[1], *( ptrptr+1 ));
  44.  
  45.                                                          /* Note 5 */
  46.      printf( "*ptrarray[0]       %c,\t**ptrarray       %c\n",
  47.                                   *ptrarray[0], **ptrarray );
  48.      printf( "ptrarray[0][4]     %c,\t*(*ptrarray + 4) %c\n\n",
  49.                                   ptrarray[0][4], *( *ptrarray+4 ));
  50.  
  51.                                                         /* Note 6 */
  52.      for (  ; strcmp( *ptrptr, ""); ptrptr++ )
  53.           printf( "%s ", *ptrptr );
  54.      printf( "\n" );
  55.                                                         /* Note 7 */
  56.      for ( ptrptr = ptrarray; strcmp( *ptrptr, "" ); ptrptr++ )
  57.           printf( "%c", **ptrptr );
  58.      printf( "\n" );
  59.      return 0;
  60. }
  61.