memcpy
功能:内存拷贝函数,从数据源拷贝num个字节的数据到目标数组函数原型:
void * memcpy ( void * destination, const void * source, size_t num );
函数参数:
destination:指向目标数组的指针
source:指向数据源的指针
num:要拷贝的字节数
使用方法:
#include<stdio.h>
#include<string.h>
struct person
{
char* name;
char* sex;
};
int main()
{
//使用memcpy拷贝字符串
char* source = "there have something";
char desc[50];
std::memcpy(desc,source,strlen(source)+1); //不能用sizeof,sizeof(str2)指的是指针的大小
std::cout<<"dec[]="<<desc<<std::endl;
//拷贝字符串中的某些字符
char* s1 = "nothing is impossible";
char s2[40];
std::memcpy(s2,s1+10,strlen(s1)+1-10);
std::cout<<"s2: "<<s2<<std::endl;
//使用memcpy拷贝字符数组
char str1[] = "made in china";
char str2[50];
std::me