memset:
memset is such a library function which is rarely used in practical. Suppose, in an array, for some certain values you have to make that true. And for any other values that will be false. In this case you have to change all the elements of the array to false first, and then you can make that true for those specific values. memset is the easiest way to do this. An example of it is given below:
bool row [10000 + 5];
bool col [10000 + 5];
memset (row, false, 10005);
memset (col, false, 10005);
memset:
void * memset ( void * ptr, int value, size_t num );
Fill block of memory Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).
Parametersptr
Pointer to the block of memory to fill.
value
Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.
num
Number of bytes to be set to the value.
Return Valueptr is returned.
Fill block of memory Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).
Parametersptr
Pointer to the block of memory to fill.
value
Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.
num
Number of bytes to be set to the value.
Return Valueptr is returned.
Example
/* memset example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] = "almost every programmer should know memset!";
memset (str,'-',6);
puts (str);
return 0;
}
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] = "almost every programmer should know memset!";
memset (str,'-',6);
puts (str);
return 0;
}
Output:
------ every programmer should know memset!