memset
write a byte to byte string
You're viewing an older version of this page (#3493). View the current version.
INTERFACE
#include <string.h> void memset ( void *b, int c, size_t len )
DESCRIPTION
The memset function writes len bytes of value c (converted to an unsigned char) to the string b.
Example 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
// allocate memory for some array x of 5 elements
char *x = (char *) malloc(5*sizeof(char));
// set all elements of x to 0
memset(x, 0, 5*sizeof(char));
// increase all elements of x by 1
for (int i = 0; i < 5; ++i)
++x[i];
// display x
for (int i = 0; i < 5; ++i)
printf("x[%d] = %d\n", i, x[i]);
// remove x from memory
free(x);
return 0;
}Output:
x[0] = 1
x[1] = 1
x[2] = 1
x[3] = 1
x[4] = 1RETURN VALUES
The memset function returns its first argument.
Compatibility
<table class="compat"> <tr><th></th><th>DOS</th><th>UNIX</th><th>Windows</th><th>ANSI C</th><th>C++ only</th></tr> <tr><td>memset</td><td>•</td><td>•</td><td>•</td><td>•</td><td></td></tr> </table>