C语言-字符串拷贝(1)strcpy

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);
}

2. 参考:

  1. https://blog.csdn.net/tigerjibo/article/details/6412759
  2. https://blog.csdn.net/xx326664162/article/details/72781528
  3. https://blog.csdn.net/weibo1230123/article/details/80382614
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值