memset(3)

fill memory with a constant byte

Section 3 manpages-dev bookworm source

Summary

memset() fills the first n bytes of a memory area with a single constant byte value. It is the standard way to initialize or clear a buffer in C, for example zeroing out a struct or array before use. It returns a pointer to the same memory area you passed in.

Tip: The fill value c is interpreted as a byte, so memset only works cleanly for byte patterns like 0 or 0xFF; you cannot use it to set an int array to an arbitrary value like 1.

Description

memset

NAME

memset - fill memory with a constant byte

LIBRARY

Standard C library (libc, -lc)

SYNOPSIS

#include <string.h>

void *memset(void s[.n], 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.

RETURN VALUE

The memset() function returns a pointer to the memory area s.

ATTRIBUTES

For an explanation of the terms used in this section, see attributes(7).

Image grohtml-42210-1.png

STANDARDS

POSIX.1-2001, POSIX.1-2008, C99, SVr4, 4.3BSD.

SEE ALSO

bstring(3), bzero(3), swab(3), wmemset(3)

Examples

char buf[256];
memset(buf, 0, sizeof(buf));

Zero out an entire buffer before using it.

struct config cfg;
memset(&cfg, 0, sizeof(cfg));

Clear all fields of a struct to zero in one call.

char line[80];
memset(line, '-', sizeof(line));

Fill a buffer with a repeated byte value (here the dash character).