PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/key_point/0612/memcpy.c

https://github.com/guolilong2012/exercise_aka
C | 37 lines | 25 code | 9 blank | 3 comment | 5 complexity | cdb69feba1b0bc4ac96a76f8b3f87561 MD5 | raw file
  1. /* memcpy.c */
  2. #include <stdio.h>
  3. #include <assert.h>
  4. /* memcpy */
  5. void *memcpy(void *dest, const void *src, int n)
  6. {
  7. assert(dest != NULL);
  8. assert(src != NULL);
  9. assert(n >= 0);
  10. if (dest == src)
  11. return dest;
  12. char *d = (char *) dest;
  13. const char *s = (const char *) src;
  14. while (n--) {
  15. *d++ = *s++;
  16. }
  17. return dest;
  18. }
  19. /* main */
  20. int main(int argc, const char *argv[])
  21. {
  22. char str[] = "hello";
  23. char s[6];
  24. memcpy(s, str, sizeof(s));
  25. printf("%s\n", s);
  26. printf("%s\n", (char *) memcpy(s, str, sizeof(s)));
  27. return 0;
  28. }