home *** CD-ROM | disk | FTP | other *** search
- /* Demonstrates strcpy(). */
-
- #include <stdio.h>
- #include <string.h>
-
- char source[] = "The source string.";
-
- main()
- {
- char dest1[80];
- char *dest2, *dest3;
-
- printf("\nsource: %s", source );
-
- /* Copy to dest1 is okay because dest1 points to */
- /* 80 bytes of allocated space. */
-
- strcpy(dest1, source);
- printf("\ndest1: %s", dest1);
-
- /* To copy to dest2 you must allocate space. */
-
- dest2 = (char *)malloc(strlen(source) +1);
- strcpy(dest2, source);
- printf("\ndest2: %s", dest2);
-
- /* Copying without allocating destination space is a no-no. */
- /* The following could cause serious problems. */
-
- /* strcpy(dest3, source); */
- }
-