標籤:ted append source string point ring bytes lock include
原型: 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個參數決定複製的長度。
1 //#define FIRST_DEMO 2 //#define SECOND_DEMO 3 //#define THIRD_DEMO 4 #define MYMEMCPY 5 //#define FORTH_DEMO 6 7 #ifdef FIRST_DEMO 8 #include <stdio.h> 9 #include <conio.h> 10 #include <stdlib.h> 11 #include <string.h> 12 int main(void) 13 { 14 char *s="Golden Global View"; 15 char d[20]; 16 system("cls"); 17 memcpy(d,s,(strlen(s)+1)); //將 s的字串複製到數組d中 18 printf("%s\n",d); 19 getch(); 20 return 0; 21 } 22 #elif defined SECOND_DEMO 23 #include <stdio.h> 24 #include <conio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 int main(void) 28 { 29 char *s="Golden Global View"; 30 char d[20]; 31 system("cls"); 32 /*從第14個字元(V)開始複製,連續複製4個字元(View)*/ 33 memcpy(d,s+14*sizeof(char),4*sizeof(char)); 34 d[4]=‘\0‘; //這個語句若不加,輸出的字元中有未初化的字元,顯示亂碼。 35 printf("%s\n",d); 36 getch(); 37 return 0; 38 } 39 #elif defined THIRD_DEMO 40 #include <stdio.h> 41 #include <conio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 int main(void) 45 { 46 char src[] = "******************************"; 47 char dest[] = "abcdefghijlkmnopqrstuvwxyz0123as6"; 48 printf("destination before memcpy: %s\n", dest); 49 memcpy(dest,src,strlen(src)); 50 printf("destination after memcpy: %s\n", dest); 51 getch(); 52 return 0; 53 } 54 #elif defined MYMEMCPY 55 #include <stdio.h> 56 #include <conio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 void *mymemcpy(void *dest,const void *src,size_t count); 60 int main(void) 61 { 62 char src[] = "******************************"; 63 char dest[] = "abcdefghijlkmnopqrstuvwxyz0123as6"; 64 printf("destination before mymemcpy: %s\n", dest); 65 mymemcpy(dest,src,strlen(src)); 66 printf("destination after mymemcpy: %s\n", dest); 67 getch(); 68 return 0; 69 } 70 71 void *mymemcpy(void *dest,const void *src,size_t count) 72 { 73 char *ret=(char *)dest; 74 char *dest_t=ret; 75 const char *src_t=(char *)src; 76 //append 77 // while(*dest_t!=‘\0‘) 78 // { 79 // dest_t++; 80 // } 81 while(count--) 82 { 83 *dest_t++=*src_t++; 84 } 85 return ret; 86 } 87 #elif defined FORTH_DEMO 88 #include <stdio.h> 89 #include <conio.h> 90 #include <stdlib.h> 91 #include <string.h> 92 struct 93 { 94 char name[40]; 95 int age; 96 }person,person_copy; 97 int main(void) 98 { 99 char myname[]="Pierre de Fermat";100 printf("sizeof(myname)=%d\n",sizeof(myname));101 printf("strlen(myname)=%d\n",strlen(myname));102 memcpy(person.name,myname,strlen(myname)+1);103 person.age=48;104 memcpy(&person_copy,&person,sizeof(person));105 printf("person_copy :%s,%d\n",person_copy.name,person_copy.age);106 getch();107 return 0;108 }109 #endif
memcpy函數-C語言