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

  1.  /* Demonstrates strcpy(). */
  2.  
  3.  #include <stdio.h>
  4.  #include <string.h>
  5.  
  6.  char source[] = "The source string.";
  7.  
  8.  main()
  9.  {
  10.      char dest1[80];
  11.      char *dest2, *dest3;
  12.  
  13.      printf("\nsource: %s", source );
  14.  
  15.      /* Copy to dest1 is okay because dest1 points to */
  16.      /* 80 bytes of allocated space. */
  17.  
  18.      strcpy(dest1, source);
  19.      printf("\ndest1:  %s", dest1);
  20.  
  21.      /* To copy to dest2 you must allocate space. */
  22.  
  23.      dest2 = (char *)malloc(strlen(source) +1);
  24.      strcpy(dest2, source);
  25.      printf("\ndest2:  %s", dest2);
  26.  
  27.      /* Copying without allocating destination space is a no-no. */
  28.      /* The following could cause serious problems. */
  29.  
  30.      /* strcpy(dest3, source); */
  31.  }
  32.