c和c++使用的內存拷貝函數,memcpy函數的功能是從源src所指的內存地址的起始位置開始拷貝n個字節到目標dest所指的內存地址的起始位置中。
void *memcpy(void *dest, const void *src, size_t n);
//memcpy.c
#include <stdio.h>
#include <string.h>
int main()
{
char* s="GoldenGlobalView";
chard[20];
clrscr();
memcpy(d,s,(strlen(s)+1));
printf("%s",d);
getchar();
return 0;
}
輸出結果:Golden Global View
example2
作用:將s中第13個字符開始的4個連續字符復制到d中。(從0開始)
#include<string.h>
int main(
{
char* s="GoldenGlobalView";
char d[20];
memcpy(d,s+12,4);//從第13個字符(V)開始復制,連續復制4個字符(View)
d[4]='\0';//memcpy(d,s+14*sizeof(char),4*sizeof(char));也可
printf("%s",d);
getchar();
return 0;
}
輸出結果:
View example3
作用:復制後覆蓋原有部分數據
#include<stdio.h>
#include<string.h>
intmain(void)
{
char src[]="******************************";
char dest[]="abcdefghijlkmnopqrstuvwxyz0123as6";
printf("destination before memcpy:%s\n",dest);
memcpy(dest,src,strlen(src));
printf("destination after memcpy:%s\n",dest);
return 0;
}
輸出結果:
destination before memcpy:abcdefghijlkmnopqrstuvwxyz0123as6 destination after memcpy: ******************************as6