cs24-24fa memset

Introduction to Computing Systems (Fall 2024)

Name

memset - fill memory with a constant byte

Synopsis

1
2
3
#include <string.h>

void *memset(void *s, int c, size_t n);

Description

The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c.

Example

1
2
3
4
5
6
7
8
9
10
#include <assert.h>
#include <string.h>

int main() {
    char s[7] = "hello!";
    
    // Fill string s with null bytes, making it an empty string.
    memset(s, '\0', sizeof(s));
    assert(strcmp(s, "") == 0);
}

Return Value

Upon completion, the memset() function returns a pointer to the memory area s.

See Also

memcpy(), memmove(), strcpy(), strncpy()