Strcpy, memcpy, and strncpy

Source: Internet
Author: User

1. strcpy Function

Strcpy provides string replication. That is, strcpy is only used for string copying. It not only copies the content of the string, but also copies the end character of the string.

Function prototype and implementation:

Char * strcpy (char * strdst, const char * strsrc) <br/>{< br/> assert (strdst! = NULL) & (strsrc! = NULL); <br/> char * address = strdst; <br/> while (* strdst ++ = * strsrc ++ )! = '/0'); <br/> return address; <br/>}

 

Note:

1. This function will be copied to the character array together with the character string ending character '/0'. Therefore, the length of the character array should be at least the length of the string plus 1.

Example:

Char STR [] = "123456 ";

Here STR is a character array, which stores a string "123456". Because the string also has an Terminator "/0", the length of this array is 7 rather than 6.

 

Char * STR = "123456"; // The length of the string is 6 (the string ends with the character '/0' and is not counted in the string length)

Char DST [7]; // The length of the character array must be at least 1 plus the character length, for example, 6 + 1 = 7.

Strcpy (DST, STR );

2. this function copies the content of a string until it encounters a character whose first character value is 0 (the character whose first character value is 0 will also be copied). Therefore, if a string or character array contains

If there is a large amount of data with a value of 0, it is not recommended to use this function for copy operations. The memcpy function is available.

 

2. memcpy Function

Memcpy provides general memory replication. That is, memcpy has no restrictions on the content to be copied, so it is widely used.

This function copies the value of a memory segment of the size to another memory segment.

The implementation is as follows:

Void * memcpy (void * memto, const void * memfrom, size_t size) <br/>{< br/> assert (memto! = NULL) & (memfrom! = NULL); // memto and memfrom must be valid <br/> char * tempfrom = (char *) memfrom; // Save the first memfrom address <br/> char * tempto = (char *) memto; // Save the first memto address <br/> while (size --> 0) // cyclically size, copy the value of memfrom to memto <br/> * tempto ++ = * tempfrom ++; <br/> return memto; <br/>}

 

 

3. Differences between strcpy and memcpy

3.1 The copied content is different.

Strcpy can only copy strings, while memcpy can copy any content, such as character arrays, integers, struct, and classes.

3.2 The replication method is different.

Strcpy does not need to specify the length. It ends with the string Terminator "/0. Memcpy decides the copy Length Based on its 3rd parameters.

3.3 different purposes.

Generally, strcpy is used to copy strings, while memcpy is generally used to copy other types of data.

 

4. strncpy

The implementation of strncpy is as follows:

Char * strncpy (char * DST, const char * SRC, size_t count) <br/>{< br/> assert (DST! = NULL) & (SRC! = NULL); <br/> char * TMP = DST; </P> <p> while (count -- & (* DST ++ = * SRC ++ )! = '/0') <br/>{< br/>/* nothing */; <br/>}</P> <p> return TMP; <br/>}< br/>

 

This function is used to copy count characters.

 

Note:

1. Count must be smaller than DST.

2. after calling this function, you must add the following sentence: DST [count] = '/0'; otherwise, it is not safe, for example, strlen and other functions require that the parameter must be a string ending with '/0.

When count is smaller than the size of SRC, the ending character '/0' of SRC will not be copied. Therefore, an ending character should be added to DST.

Example:

Char * STR = "123456"; </P> <p> char DST [7]; </P> <p> int COUNT = 6; </P> <p> strncpy (DST, STR, count); // COUNT = 6 must be smaller than the DST length (7 ). </P> <p> DST [count] = '/0'; <br/>

 

 

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.