Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

memcpy.c (808B)


      1 
      2 
      3 #include "../stdint.h"
      4 #include "string.h"
      5 
      6 void *memcpy (void *dest, const void *src, size_t n) {
      7 
      8     // Safety check
      9     if (src == (void *)'\0' || dest == (void *)'\0') return dest;
     10     if (n == 0) return dest;
     11 
     12     unsigned char *dst = (unsigned char *)dest;
     13     const unsigned char *srrc = (const unsigned char *)src;
     14 
     15     size_t i = 0; // initialize i to the beginning as a counter
     16 
     17     while (1) {
     18         if (i == n) { // check if i == n that means loop has completed filling "n" bytes of p with x
     19             break;
     20         }
     21         *dst = *srrc; // dereference 'dst' and set it to dereferenced 'srrc'
     22         dst++; // increment address of pointer by one byte
     23         srrc++; // increment read-only source pointer
     24         i++; // increment i to move for next 'n' byte
     25     }
     26 
     27     return dest;
     28 }