Simvik

Implement memset

Fill a block of memory with one byte value. An easy embedded C problem (Pointers), run and graded in the browser.

easy · Pointers · libc · void-pointer

Implement memset: set each of the first n bytes at dst to value converted to unsigned char, and return dst.

Small bootloaders and startup code often cannot link the C library and carry their own version.

Starter code

void *my_memset(void *dst, int value, size_t n) {
  // Fill n bytes at dst with (unsigned char)value and return dst.
  return dst;
}

Example tests

Fill part of a string

char buf[8] = "abcdefg";
my_memset(buf + 1, 'x', 3);
printf("%s\n", buf);

Expected output:

axxxefg

Returns the destination

uint8_t buf[4] = { 1, 2, 3, 4 };
printf("%d %u %u %u\n", my_memset(buf, 0, 2) == buf, buf[0], buf[1], buf[2]);

Expected output:

1 0 0 3

Learn it first

  • Pointers · Addresses, arithmetic, const, void pointers and object lifetime.

More Pointers problems