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:

  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  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值