Memset initialization array, memset Array

Source: Internet
Author: User
Tags integer numbers

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;
}
//---------------------------------------------------------------------------

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.