memcpy关于拷贝字符串终止字符的问题

一、程序测试

#include <iostream>
#include <cstring>
using namespace std;

int main( void )
{
    /* gerald test start */
    
    cout << "***** gerald test start *****" << endl;
    char acTmp1[10] = "123456789";
    char acTmp2[5] = "11";
    
    memcpy(acTmp1, acTmp2, strlen(acTmp2));
    cout << acTmp1 <<endl;
    
    memcpy(acTmp1, acTmp2, sizeof(acTmp2));
    cout << "Number of copy bytes: " << sizeof(acTmp2) << endl;
    cout << acTmp1 <<endl;
    
    cout << "The length of acTmp2: " <<strlen(acTmp2) << endl; 
	cout << "***** gerald test end *****" << endl;
   /* gerald test end*/

	system( "PAUSE" );
 	return 0;
}

二、输出结果

***** gerald test start *****
113456789
The number of copy bytes: 5
11
The length of acTmp2: 2
***** gerald test end *****
请按任意键继续. . .

三、结论

memcpy有没有拷贝字符串最后的’\0’字符和函数本身无关系,memcpy支持拷贝字符串末尾字符,能不能拷贝字符串的末尾字符取决于传输的参数size_t n的值

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值