Memset initialization array, memset Array
Memset is a function used to initialize a memory area. Its header file is <string. h>. If you have used memset frequently before, sort it out.
C ++ Reference defines memset:
void * memset ( void * ptr, int value, size_t num );
The parameters of memset are defined as follows:
Ptr: Pointer to the block of memory to fill.
Value: Value to be set. The value is passed asInt, But the function fills the block of memory usingUnsigned charConversion of thisValue.,
Num: Number of bytes to be set toValue.
In the parameter description, the value is transmitted according to the int type, but the value is converted to unsigned char for filling during filling. Therefore, when using memset for memory initialization, the value should be a byte value.
Initialize the memory area of the int type. The ptr type is int *. For example:
1. initialize the memory to 0
memset(ptr,0,sizeof(ptr));
2. initialize the memory to-1
memset(ptr,0xff,sizeof(ptr));
Because the value is 0xff Based on the bytes, each byte in the memory will be filled with 0xff. If the int value is 1, the int value is-1.
3. initialize the memory to the maximum value.
memset(ptr,0x3f,sizeof(ptr));
The initial int value of this initialization code is 1061109567.
If you want to initialize the int memory region to 1, memset will be powerless.
The memset function initializes the int.
Memset memcpy is a string processing function, not for the int type.
0 equals NULL
So memset 0 is okay.
However, other numbers are considered as characters and do not correspond to integer numbers.
The system computes the character into an integer and stores it. Therefore, it gets a strange number.
Can the memset function initialize the structure array?
Yes, for example:
//---------------------------------------------------------------------------
# Include <stdio. h>
# Include <string. h>
Typedef struct {
Int;
Int B;
} Sta;
Int main (void)
{
Sta a [10];
Memset (a, 0, sizeof (sta) * 10);/* initialize each member of each element in array a to 0 */
Return 0;
}
//---------------------------------------------------------------------------