The copy function of C + + string function

Source: Internet
Author: User

Suddenly found the lack of a systematic understanding of string functions, so spent a little time dedicated to the record, in order to facilitate their own and those who need to use.

Header file for C + + String functions: String.h

There are 4 main replication functions, as follows:

1, char * strcpy (char* destination,const char * source);

2, char* strncpy (char* destination,const char* source,size_t num);

3, void * memcpy (void* destination,const void* source,size_t num);

4, void * Memmove (void* destination,const void* source,size_t num);

Function and Usage Description:

1. strcpy: Copy the C string (including the trailing character) indicated by the source pointer to the area indicated by the destination pointer. The function does not allow the region of source and destination to overlap, and in order to avoid overflow, the destination area should be at least as large as the source area.

2. strncpy: Copy the first num character of source to destination. If a null character (' Num-n ') is encountered, and no more than NUM characters are present, a null character appended to the destination is used (n is the number of non-null characters that were already present before the null character was encountered). Note: It is not added to the end of destination, but follows the character copied from source. The following examples illustrate:

Char des[] = "Hello,i am!";

Char source[] = "Abc\0def";

strncpy (des,source,5);

At this point, the Des Region is this: a,b,c,\0,\0,i, spaces, a,m,!

\0,\0 is not added to the back of!

Here, it's important to note that strcpy only copies to the null character and ends.

3. memcpy: Copies the first num characters from the source area to destination. The function does not check for a null character (the null character is treated as a normal character), which means that the characters of Num characters will be copied over. This function does not introduce additional null characters, that is, if there are no null characters in num characters, there are no null characters in the corresponding sequence of characters in destination. Difference from strcpy: allows the characters following the null character in source to be copied to the destination, while strcpy and strncpy are not.

4, Memmove: The same function with memcpy, the difference is that the memmove allows the destination and source areas overlap. While the other three functions are not allowed.

Example: char str[] = "This is a test!";

Memmove (str+2,str+10,4);

At this point, Str becomes: Thtests a test!

The copy function of C + + string function

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.