strcat和strncat

strcat

我们依旧先看一下strcat函数在msdn中的解释:

The strcat function appends strSource to strDestination and terminates the resulting string with a null character. The initial character of strSource overwrites the terminating null character of strDestination. No overflow checking is performed when strings are copied or appended. The behavior of strcat is undefined if the source and destination strings overlap.

strcat函数将strSource追加到strDestination,并用空字符终止结果字符串。strSource的初始字符覆盖strDestination的终止空字符。复制或追加字符串时不执行溢出检查。如果源字符串和目标字符串重叠,strcat的行为是未定义的。

这里需要注意以下几点:

	1.源字符串必须以'\0'结尾
	2.目标空间需要足够的大,使其可以容纳原字符串的内容
	3.目标空间必须是可修改的
	4.如果源字符串中没有'/0',那么复制的结果是未定义的,由于strncat不会执行溢出检查,所以并不会报错,但是程序可能会崩溃
	5.如果源字符串和目标字符串重叠的话,strcat的行为是未定义的,也就是不具有实际价值

那么知道了这些,下面我们自己来试着动手实现一下strcat函数:

char* MyStrcat(char* dst, const char* src)
{
   
	char* p = dst;
	assert(dst && src);
	while (*dst != 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值