C language function memset Detailed

C language function memset Detailed

Memset action () of : filling a given value in a period memory block, typically used initialize the array and the array is cleared .

It is a direct memory operation, mem i.e. "memory" (Memory) mean. The prototype of this function is:

# include <string.h>
void *memset(void *s, int c, unsigned long n);

Function Function : the pointer points to the variables s of the first n bytes of a memory cell with "integer" c Alternatively, note that c is an int. s is type void * pointer variable, it can be initialized to any type of data.

 

memset () function Explanation:

memset general use "0" to initialize the memory cell, and usually to an array or structure is initialized. General variables are as char, int, float, double etc., variables can be initialized directly, there is no need to use memset. If memset words but seemed in trouble.
Of course, the array can be initialized directly, but it is the fastest way to clear memset initializing the array or structure larger body, direct memory because it is operating.
Then someone will ask: "string array is not the best use '\ 0' initialize it so it can be initialized with a string array that is to memset parameter c can be assigned to '\ 0' it???"
Can of. Although the requirement parameter c is an integer, integer and character but is interconnected. But the assignment is '\ 0' and 0 are equivalent, because the character '\ 0' in memory is 0. So initialized to 0 memset the end identifier also has the role of '\ 0', so we usually write "0."
third value of the parameter n is generally a function of memset with the sizeof () Gets, so more professional. Note that if the memory unit of the pointer variable points cleared initialization, so be sure to initialize the pointer variable that must first let it points to a valid address. And such as when the memory cell pointed to by p memset to be initialized with a pointer variable, n do not write sizeof (p), which is often a novice mistake. Because p is a pointer variable, no matter what type of variable p points, the value of sizeof (p) is 4.

 

Program example :

The include # <stdio.h> 
# the include < String .h>
 int main ( void ) 
{ 
    int I;   // loop variable 
    char STR [ 10 ];
     char * P = STR; 
    Memset (STR, 0 , the sizeof (STR)) ;   // only write sizeof (str), can not write the sizeof (P) 
    for (I = 0 ; I < 10 ; ++ I) 
    { 
        the printf ( " % D \ on the x20 " , STR [I]); 
    } 
    the printf ( " \ the n- ");
    return 0;
}
Depending memset functions, the output is different, divided into the following situations: 
memset (P, 0 , the sizeof (P));   // size of an address is 4 bytes 
0  0  0  0 - 52 is - 52 is - 52 is - 52 is - 52 is - 52 is 

Memset (P, 0 , the sizeof (* P));   // * P is represented by a character variable, only one byte 
0 - 52 is - 52 is - 52 is - 52 is - 52 is - 52 is - 52 is - 52 - 52 

memset (the p-,0 , the sizeof (STR));
 0  0  0  0  0  0  0  0  0  0 

Memset (STR, 0 , the sizeof (STR));
 0  0  0  0  0  0  0  0  0  0 

Memset (P, 0 , 10 );   / / direct write 10 also OK, but not professional 
0  0  0  0  0  0  0  0  0  0

 

Guess you like

Origin www.cnblogs.com/-wenli/p/11491127.html