Chinaunix首页 | 论坛 | 博客
  • 博客访问: 368929
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: LINUX

2011-04-22 09:33:13

  • What is the difference between memcpy and strcpy?
strcpy ends copying of data when it reaches NULL character (Say, '\0' or 0) and then copies NULL at the end of destination data. It is specifically used to copy strings (char[]).

memcpy can be used to copy any type of data (void*). It terminates at the byte position specified by the third parameter.

  1. #include <stdio.h>
  2. #include <string.h>

  3. void scpy(char *des, char *src)
  4. {
  5.         while (*src)
  6.                 *des++ = *src++;
  7.         *des = '\0';
  8. }

  9. void mcpy(char *des, char *src, int size)
  10. {
  11.         int i;
  12.         for (i=0; i < size; i++)
  13.                 des[i] = src[i];
  14. }

  15. int
  16. main(void)
  17. {
  18.         char *s = "hello world";
  19.         char d[12];
  20.         scpy(d, s);
  21.         printf("%s\n", d);
  22.         mcpy(d, s, strlen(s));
  23.         printf("%s\n", d);

  24.         return (0);
  25. }
阅读(1680) | 评论(2) | 转发(0) |
0

上一篇:程序编译步骤

下一篇:悬挂指针

给主人留下些什么吧!~~

onezeroone2011-04-22 13:19:22

GFree_Wind: 对于C库的实现,区别可不止这些.....
Thank you for your comment.
这里只是大概描述了strcpy和memcpy的不同,更多细节会在以后补充。

GFree_Wind2011-04-22 12:26:32

对于C库的实现,区别可不止这些