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

  1. /*                        arrayptr.c
  2.  *
  3.  *   Synopsis  - Prints information about the address, the
  4.  *               sizeof(), and the contents of an array, using 
  5.  *               both array and pointer notation.
  6.  *
  7.  *   Objective - To illustrate the relationship between pointers
  8.  *               and arrays and to demonstrate some of the
  9.  *               different methods to access array elements.
  10.  */
  11.  
  12. /* Include Files */
  13. #include <stdio.h>
  14.  
  15. int main( void )
  16. {
  17.                                                         /* Note 1 */
  18.      char   demoarray[5] = {'D', 'E', 'M', 'O', '!'};
  19.      char   *demoptr = demoarray;
  20.      int    i;
  21.                                                              
  22.                                                         /* Note 2 */
  23.      printf( "demoarray is %x.\n", demoarray );
  24.      printf( "sizeof( demoarray ) is %d.\n", sizeof( demoarray ) );
  25.      printf( "sizeof( demoarray[0] ) is %d.\n", sizeof( demoarray[0] ) );
  26.  
  27.                                                         /* Note 3 */
  28.      printf( "\ndemoptr is %x.\n", demoptr );
  29.      printf( "sizeof(demoptr) is %d.\n", sizeof( demoptr ) );
  30.      printf( "sizeof( *demoptr ) is %d.\n", sizeof( *demoptr ) );
  31.  
  32.      printf( "\ni\tdemoarray[i]\t*(demoarray+i)\t*demoptr\n" );
  33.      printf( "-\t------------\t--------------\t--------\n" );
  34.      for ( i = 0; i < 5; i++, demoptr++ )               /* Note 4 */
  35.                                                         /* Note 5 */
  36.      printf( "%d\t     %c     \t     %c     \t    %c\n",
  37.                     i, demoarray[i], *( demoarray + i ), *demoptr );
  38.      return 0;
  39. }
  40.