I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
ComputingCString.h

memset

Write a byte to byte string
+ View other versions (3)

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:
Example - Write a byte to byte string
Workings
#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;
}
Solution
Output:
x[0] = 1
x[1] = 1
x[2] = 1
x[3] = 1
x[4] = 1

Return Values

The memset function returns its first argument.

Compatibility

DOSUNIXWindowsANSI CC++ only
memset