1. 程式人生 > >C語言之memcpy函式

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