memset
Write a byte to byte string
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