table of Contents
Introduction to memset function
Concept:
memset is the initialization function of C/C++ language in the computer. The function is to set all the contents of a certain block of memory to the specified value.
This function usually initializes the newly applied memory.
That is: the
function is to fill a given value in a memory block, it is the fastest way to clear a larger structure or array.
Required header file: <string.h>
Function prototype: void *memset(void *s, int ch, size_t n);
Function explanation: replace the n bytes (typedef unsigned int size_t) behind the current position in s with ch and return s.
Syntax format: memset(array name, value, sizeof(array name) );
Note: The memset function initializes the memory block in bytes, so it cannot be used to initialize the int array to values other than 0 and -1.
This is because memset uses byte assignment, that is, assigning the same value to each byte, so that the 4 bytes that make up the int type will be assigned the same value.
Since the two's complement of 0 is all 0 and the two's complement of -1 is all 1, it is not easy to make mistakes.
Usage case
When we assign other data to it, you will find that there will be errors.
As shown below: