Manipulating Already-Allocated Memory

Name:
memset()
System call or function: Function Links: Online Manual | Course Packet
What it does: sets each of
n
bytes starting at memory location
s
(so, from
s
to
s + n - 1
) to the character
c
(e.g.: for
c = 'A'
, the memory starting at
s
will include
"AAAA..."
).
What libraries you must include:
#include <string.h> // Defines memset().
Syntax:
void * memset (void *s, int c, size_t n);
Description of arguments:
s
: a pointer to some allocated memory (doesn't need to be the start.)
c
: the character you want to set as the content of each of the bytes from from
s
to
s + n - 1
.
n
: the number of characters you want to set to
c
.
What type it returns:
void*
On success: It returns its 1st argument: the pointer
s
.
On failure: If
s
is an invalid pointer, or if
s + n - 1
is beyond the end of the allocated memory, the behavior of
memset()
is undefined.
If failure, does it set
errno
?
No
Quick example:
memset (arr, '\0', 10); // Setting 10 chars starting at 'arr' to the null character.
Other variations: N/A