HP 3000 Manuals

strcpy [ HP C/iX Library Reference Manual ] MPE/iX 5.0 Documentation


HP C/iX Library Reference Manual

strcpy 

Copies the contents of s2 into s1.

Syntax 

     #include <string.h>
     char *strcpy(char *s1, const char *s2);

Parameters 

s1            A pointer to the target string.

s2            A pointer to the source string.

Return Values 

x             The value of s1.

Description 

In the strcpy function, s2 is a character pointer to the string to be
copied, and s1 is a character pointer to the beginning of the string into
which the contents of string s2 are copied.  The strcpy function copies
the entire string, up to and including the first null encountered.  Use
strlen() and memmove() rather than strcpy() if the string at s2 overlaps
s1; otherwise, the behavior is undefined.

Examples 

The following program uses the strcpy function and the strcat function to
build a character string representing the lowercase alphabet, one
character at a time:

        #include <stdio.h>
        main()
        {
           int b = 'b', z = 'z', i;
           char alpha[30], chr[4];

           chr[1] = NULL;
           strcpy(alpha, "a");
           printf("%s\n", alpha);

           for(i = b; i <= z; i++) {
                chr[0] = i;
                strcat(alpha, chr);
                printf("%s\n", alpha);
           }
        }

The array chr is always going to be a two-character array consisting of
the next character in the alphabet followed by a null character.  Thus,
the second element of chr is set to a null character early in the
program.  The first chr element is then successively set to the next
lowercase character in the for loop, and the resulting two-character
string is concatenated onto the end of the alphabet assembled so far in
alpha.  Note the use of strcpy() to initialize alpha.  Remember that C
transforms one or more characters enclosed in double quotation marks into
a character pointer to those characters followed by a null character.
Thus, the strcpy() statement above copies the character 'a' followed by a
null character into alpha.

There are some things to be aware of when using strcpy(), strncpy(),
strcat(), and strncat().  These functions all modify string s1 in some
way, but none of them check for overflow in that string.  Therefore, be
sure there is enough room in s1 to hold the added or copied characters
plus a terminating null character.  Also, be sure you use a character
array for s1 (not just a character pointer), especially when using strcat
or strncat.  This is because an explicitly declared array can have
sufficient memory allocated to it to contain all of its elements, but a
character pointer simply points to a single location in memory.
Concatenating a string to the end of a string contained in an array is
guaranteed to work if the array is large enough.  However, concatenating
a string to a string of characters referenced by a simple character
pointer is dangerous, because the concatenated characters could overwrite
data in memory.  For example,

        char array[100], *ptr = "abcdef";
             :
        strcat(array, ptr);

works fine, because you are guaranteed that 100 storage elements have
been set aside for the array.  However,

        char *ptr1 = "abcdef", *ptr2 = "ghijkl";
             :
        strcat(ptr1, ptr2);

will not work.  Although C makes sure there is enough room for the
initializing strings ("abcdef" and "ghijkl" in this example), there are
no guarantees that there is enough room to add characters to the end of
one of these strings.  Therefore, the last fragment could easily
overwrite valid data occurring after the string pointed to by ptr1.

Because string s2 is not modified, you can use arrays or character
pointers for this item with no ill effects.

See Also 

memmove(), strlen(), strcat(), strncat(), strncpy(), ANSI C 4.11.2.3,
POSIX.1 8.1



MPE/iX 5.0 Documentation