一、程序测试
#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的值。