char* strncpy(char* destination ,const char*source,size_t num)
Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.
No null-character is implicitly appended at the end of destination if source is longer than num. Thus, in this case, destination shall not be considered a null terminated C string (reading it as such would overflow).
destination and source shall not overlap (see memmove for a safer alternative when overlapping).
- 当source中的字节小于num个时候,
'\0'
其实包含在了source所在的字符串中:将 source中 num个字节放入到 destination内存中,如果source中没有num个字节,那么填充0到destination中。 - 如果num的长度小于source的长度,那么会在source中截取num个字节放入到destination中,destnation的结尾没有没有
\0
符号。 - destination与source之间不应该有重叠。
- num不应该大于destnation的长度,不然容易丢失数据。