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 >
Wrap
C/C++ Source or Header
|
1993-10-16
|
733b
|
32 lines
/* 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); */
}