strncpy字符串拷贝函数

摘自linux内核4.11.1 源码string.c
linux/lib/string.c
Copyright (C) 1991, 1992  Linus Torvalds

头文件:#include <string.h>
作用:将源字符串src复制count个字节至目标存储区dest;如果源src超过count个字节,结果不是NUL终止的,在src的长度小于count的情况下,dest的其余部分将用%NUL填充。
参数:
dest:目标存储区
src:源字符串
count:复制的最大字节数
返回值:
dest 目标存储区的地址
/**
 * strncpy - Copy a length-limited, C-string
 * @dest: Where to copy the string to
 * @src: Where to copy the string from
 * @count: The maximum number of bytes to copy
 *
 * The result is not %NUL-terminated if the source exceeds
 * @count bytes.
 *
 * In the case where the length of @src is less than  that  of
 * count, the remainder of @dest will be padded with %NUL.
 *
 */
char *strncpy(char *dest, const char *src, size_t count)
{
	char *tmp = dest;


	while (count) {
		if ((*tmp = *src) != 0)
			src++;
		tmp++;
		count--;
	}
	return dest;
}


发布了2 篇原创文章 · 获赞 0 · 访问量 3552
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览