memset
write a byte to byte string
You're viewing an older version of this page (#4061). 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
Workings
\code #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
", i, x[i]); // remove x from memory free(x); return 0; } \endcode
Solution
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
| DOS | UNIX | Windows | ANSI C | C++ only | |
|---|---|---|---|---|---|
| memset | • | • | • | • |