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

  1. /*                               strngerr.c
  2.  *
  3.  *   Synopsis  - Initializes two strings and displays the 
  4.  *               strings and their locations. Displays the
  5.  *               strings again after concatenating the
  6.  *               second string to the end of the first one.
  7.  *
  8.  *   Objective - To demonstrate a common error in memory 
  9.  *               allocation when using the string library 
  10.  *               functions.
  11.  */
  12.  
  13. /* Include Files */
  14. #include <stdio.h>
  15. #include <string.h>
  16.  
  17. int main( void )
  18. {
  19.      char *string1 = "I know an old lady who swallowed a ";
  20.      char *string2 =
  21.           "spider that wiggled and jiggled and tickled inside her";
  22.  
  23.      printf( string1 );
  24.      printf( string2 );
  25.                                                             /* Note 1 */
  26.      printf( "\nstring1 begins at %p, and ends at %p.\n",
  27.                            string1, string1 + strlen( string1 ) );
  28.      printf( "string2 begins at %p, and ends at %p.\n\n",
  29.                            string2, string2 + strlen( string2 ) );
  30.                                                             /* Note 2 */
  31.      strcat( string1, " horse.  She's dead of course." );   /* ERROR */
  32.      printf( string1 );
  33.      printf( string2 );
  34.      printf( " \n" );
  35.      return 0;
  36. }
  37.  
  38.  
  39.