把 src 所指向的字符串复制到 dest,最多复制 n 个字符。当 src 的长度小于 n 时,dest 的剩余部分将用空字节填充。
注意:
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).
source长度小于num或者拷贝的字符串还没到达num时就遇到结束符'\0',会用0填充,直到写完num个字符。
source长度大于num时,不会在字符串末尾加结束符'\0';
char* strncpy(char* destination, const char* source , size_t num)
{
assert(destination && source );
char *tmp = destination;
while(num)
{
if ((*tmp = *source ) != 0)
{
source ++;
}
tmp++;
num--;
}
return destination;
}