首页 > 代码库 > memcpy() -- 拷贝内存函数
memcpy() -- 拷贝内存函数
定义函数: void *memcpy(void *dest, const void *src, size_t n)
函数说明: memcpy()用来拷贝src所指的内存内容前n个字节到dest所指的内存地址上。与strcpy()不同的是,memcpy()会完整的复制n个字节,不会因为遇到字符串结束‘\0‘而结束
返回值: 返回指向dest的指针
附加说明:指针src和dest所指的内存区域不可重叠
--------------------------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main()
{
char *s="Golden Global View";
char d[20];
memcpy(d,s,strlen(s));
d[strlen(s)]=0;
printf("%s\n",d);
system("pause");
}
------------------------------------------------------------------------------------------------------------------------------------------------
strcpy 与 memcpy
------------------------------------------------------------------------------------------------------------------------------------------------
main()
{
char a[30] = "string (a)";
char b[30] = "hi\0zengxiaolong";
int i;
strcpy(a, b); //a[30] = "hi\0ing (a)"
printf("strcpy():");
for(i = 0; i < 30; i++)
printf("%c", a[i]); //hi ing (a)
system("pause");
memcpy(a, b, 30); //a[30] = "hi\0zengxiaolong"
printf("\nmemcpy():");
for(i = 0; i < 30; i++)
printf("%c", a[i]); //hi zengxiaolong
printf("\n i = %d\n", i); //30
system("pause");
}
---------------------------------------------------------------------------------------------------------------------------------------------
memcpy() -- 拷贝内存函数