#include <string.h> char *strncpy(char *to, const char *from, size_t maxlen);
strncpy
copies characters from the second argument string (from
)
to the first argument string (to
) until a terminating-null
character is found or until the number of characters specified by maxlen
have been copied. If the maximum number of characters is
reached, a terminating-null character is not added. If fewer than maxlen
characters are copied, the to
string is padded with enough
null characters to bring the total number of characters copied to maxlen
.
to
string.
to
and from
areas overlap, the effect of strncpy
is
not defined.
If the maxlen
value is 0, no characters are copied. If the value
is negative, it is interpreted as a very large unsigned number,
probably causing massive overlay of memory.
Note:
At the conclusion of a call to strncpy
, the target
string may not be null terminated.
strncpy
with
more than a single null.
#include <string.h> #include <stdio.h> main() { char *phrase = "It is almost 4:30 in the afternoon."; char after[11] ; char *colon; colon = strchr(phrase, ':'); if (!colon) puts("No colon found in string."); else{ strncpy(after, colon+1, 10); /* Copy up to 10 characters */ /* after a colon. */ after[10] = '0'; printf("Text following colon is - %sn", after); /* should print "30 in the ". */ } return; }
memcpy
, strncat
, strcpy
Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.