C語言之memcpy函數

【FROM MSDN && 百科】

原型:  void *memcpy(void *dest, const void *src, size_t n);

#include<string.h>

功能:從源src所指的內存地址的起始位置開始拷貝n個字節到目標dest所指的內存地址的起始位置中

Copies the values of num bytes from the location pointed by source directly to the memory block pointed by destination.

The function does not check for any terminating null character in source - it always copies exactly num bytes.


size_t is an unsigned integral type.

返回值:.src和dest所指內存區域不能重疊,函數返回指向dest的指針

strcpy和memcpy區別如下:

1.複製的內容不同。strcpy只能複製字符串,而memcpy可以複製任意內容,例如字符數組、整型、結構體、類等。

2.用途不同。通常在複製字符串時用strcpy,而需要複製其他類型數據時則一般用memcpy

3.複製的方法不同。strcpy不需要指定長度,它遇到被複制字符的串結束符"\0"才結束,所以容易溢出。memcpy則是根據其第3個參數決定複製的長度。


DEMO:

//#define FIRST_DEMO
//#define SECOND_DEMO
//#define THIRD_DEMO
#define MYMEMCPY      
//#define FORTH_DEMO

#ifdef FIRST_DEMO
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
	char *s="Golden Global View";
	char d[20];
	system("cls");
	memcpy(d,s,(strlen(s)+1));  //將 s的字符串複製到數組d中
	printf("%s\n",d);
	getch();
	return 0;
}
#elif defined SECOND_DEMO
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
	char *s="Golden Global View";
	char d[20];
	system("cls");
	/*從第14個字符(V)開始複製,連續複製4個字符(View)*/
	memcpy(d,s+14*sizeof(char),4*sizeof(char));
	d[4]='\0';   //這個語句若不加,輸出的字符中有未初化的字符,顯示亂碼。
	printf("%s\n",d);
	getch();
	return 0;
}
#elif defined THIRD_DEMO
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main(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);
	getch();
	return 0;
}
#elif defined MYMEMCPY
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
void *mymemcpy(void *dest,const void *src,size_t count);
int main(void)
{
	char src[] = "******************************";
	char dest[] = "abcdefghijlkmnopqrstuvwxyz0123as6";
	printf("destination before mymemcpy: %s\n", dest);
	mymemcpy(dest,src,strlen(src));
	printf("destination after mymemcpy: %s\n", dest);
	getch();
	return 0;
}

void *mymemcpy(void *dest,const void *src,size_t count)
{
	char *ret=(char *)dest;
	char *dest_t=ret;
	const char *src_t=(char *)src;
	//append
// 	while(*dest_t!='\0')
// 	{
// 		dest_t++;
// 	}
	while(count--)
	{
		*dest_t++=*src_t++;
	}
	return ret;
}
#elif defined FORTH_DEMO
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct  
{
	char name[40];
	int age;
}person,person_copy;
int main(void)
{
	char myname[]="Pierre de Fermat";
	printf("sizeof(myname)=%d\n",sizeof(myname));
	printf("strlen(myname)=%d\n",strlen(myname));
	memcpy(person.name,myname,strlen(myname)+1);
	person.age=48;
	memcpy(&person_copy,&person,sizeof(person));
	printf("person_copy :%s,%d\n",person_copy.name,person_copy.age);
	getch();
	return 0;
}
#endif


發佈了90 篇原創文章 · 獲贊 68 · 訪問量 66萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章