C language memset () function, C language memset Function

Source: Internet
Author: User

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

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.