C language memset () function, C language memset Function
1) Name: memset () function
2) alias: char type initialization Function
3) function: sets all the content of each byte in the memory to the ASCII value specified by ch. The block size is specified by the third parameter, this function usually initializes the newly applied memory.
4) usage: void * memset (void * s, char ch, unsigned n );
5) function: Fill in a given value in a memory block. It is the fastest way to clear large struct or array.
6) sample code:
# Include
# Include
# Include
Int main (void)
{
Char buffer [] = "Hello world/n ";
Printf ("Buffer before memset: % s/n", buffer );
Memset (buffer, '*', strlen (buffer ));
Printf ("Buffer after memset: % s/n", buffer );
Return 0;
}
Output result:
Buffer before memset: Hello world
Buffer after memset :***********
7) deep connotation of memset (): Used to set all memory spaces to a specific character. It is generally used to initialize the defined string to 'memset (a, '/0 ', sizeof (a); memcpy is used for memory copying. You can use it to copy any data type object and specify the length of the copied data. For example, char a [100], B [50]; memcpy (B, a, sizeof (B); note that if sizeof (a) is used, the memory address of B may overflow. Strcpy can only copy strings. It ends copying when '/0' is encountered. For example, char a [100], B [50]; strcpy (a, B ); if strcpy (B, a) is used, check whether the length of the string in a (before the first '/0') exceeds 50 bits. If it exceeds, this will cause memory address overflow of B.
References: sogou encyclopedia, C language Column