memset.c (639B)
1 /* SPDX-header: */ 2 3 #include "../stdint.h" 4 #include "string.h" 5 6 7 void *memset (void *ptr, int x, size_t n) { 8 9 unsigned char *p = (unsigned char *)ptr; 10 11 size_t i = 0; // initialize i to the beginning as a counter 12 while (1) { 13 14 if (i == n) { // check if i == n that means loop has completed filling "n" bytes of p with x 15 break; 16 } 17 18 *p = (unsigned char)x; // dereference 'p' and set it to 'x' by casting the int x to a char 19 20 p++; // increment address of pointer by one byte 21 i++; // increment i to move for next 'n' byte 22 } 23 24 return ptr; // return original pointer passed 25 }