现在的位置: 首页 > 综合 > 正文

strncpy

2018年03月21日 ⁄ 综合 ⁄ 共 541字 ⁄ 字号 评论关闭

 1. strncpy功能说明:

    The strncpy function copies the initial count characters of strSource to strDest
and returns strDest. If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string. If count is greater than the length
of strSource, the destination string is padded with null characters up to length count.
 
The behavior of strncpy is undefined if the source and destination strings overlap.

2. 自定义strncpy实现:

char * my_strncpy( char * dest, const char * source, int count )
{
  char *p = dest;
  while (count && (*p++ = *source++)) 
    count--;
  while(count--)
  *p++ = '\0';
  return dest;
}

抱歉!评论已关闭.