char
*
strcpy(
char * restrict dst
, const char * restrict src
)
char
*
strncpy(
char * restrict dst
, const char * restrict src
, size_t len
)
)
and
strncpy(
)
functions
copy the string
src
to
dst
(including the terminating
`\0'
character).
The
strncpy()
function copies not more than
len
characters into
dst
,
appending
`\0'
characters if
src
is less than
len
characters long, and
not
terminating
dst
if
src
is
len
or more characters long.
)
and
strncpy(
)
functions
return
dst
.
chararray
''
to
``
abc\0\0\0
''.
(void)strncpy(chararray, "abc", 6);
The following sets
``chararray
''
to
``abcdef
''
and does
not
nul-terminate
chararray
because the source string is >= the length parameter.
strncpy()
only
nul-terminates the destination string when the length of the source
string is less than the length parameter.
(void)strncpy(chararray, "abcdefgh", 6);
The following copies as many characters from
input
to
buf
as will fit and nul-terminates the result.
Because
strncpy()
does
not
guarantee to nul-terminate the string itself, we must do this by hand.
char buf[BUFSIZ];
(void)strncpy(buf, input, sizeof(buf) - 1);
buf[sizeof(buf) - 1] = '\0';
Note that strlcpy(3) is a better choice for this kind of operation. The equivalent using strlcpy(3) is simply:
(void)strlcpy(buf, input, sizeof(buf));
)
and
strncpy(
)
functions
conform to
ISO/IEC 9899:1999 (``ISO C99'') .