C语言-字符串拷贝(1)strcpy
https://blog.csdn.net/lqy971966/article/details/102675770
C语言-字符串拷贝(2)strncpy
https://blog.csdn.net/lqy971966/article/details/102676298
C语言-字符串拷贝(3)strlcpy
https://blog.csdn.net/lqy971966/article/details/102689950
1. strcpy
1 定义:
strcpy把含有’\0’结束符的字符串复制到另一个地址空间,返回值的类型为char* ,strcpy 是依据 /0 作为结束判断的。
2 原型:
char * strcpy(char* dest, const char *src);
3 功能:
把从src地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间
4 说明:
src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
5 缺点:
如果 dst 的空间不够,则会引起 buffer overflow缓冲区溢出。
strcpy只是复制字符串,但不限制复制的数量,很容易造成缓冲溢出
6 实现
char * strcpy(char *dst, const char *src)
{
if ((NULL==dst) || (NULL==src))
{
return NULL;
}
char *save = dst;
/*
while((*dst++ = *src++)!='\0');
赋值表达式返回左操作数,所以在赋值'\0'后,循环停止
*/
for (; (*dst= *src) != '/0'; ++src, ++dst)
;
return(save);
}