String processing-copy, merge, and compare (strcpy, strcat, strcmp)

Source: Internet
Author: User

Summary of common API functions:

Copy: strcpy, memcpy, sprintf; strncpy, _ snprintf

Merge: strcat, sprintf; strncat, _ snprintf

Comparison: strcmp, memcmp; strncmp

 

Here, we only need to analyze one type of related functions, and at the same time we can understand the functions of other classes.

 

First, let's take a look at strcpy, memcpy, and sprintf functions. These three functions can be used to copy strings.

Strcpy: copy a string.

Function prototype ::

Char * strcpy (
Char * strdestination,
Const char * strsource
);

Strcpy is a copy string and ends with a/0 mark (that is, once the memory address copy process with a data value of 0 is stopped)

Memcpy: Copies bytes between buffers.

Function prototype:

Void * memcpy (
Void * DEST,
Const void * SRC,
Size_t count
);

Memcpy copies the memory data of a specified size N after the source and target are specified, regardless of the copied content (not limited to words ).

Character)
Sprintf: Write formatted data to a string.

Function prototype:

Int sprintf (
Char * buffer,
Const char * Format [,
Argument]...
);

Sprintf a string copy that can be given a format

 

 

Then let's take a look at the differences between the three functions:

Strcpy can only process strings. If you copy strings with special characters, you can only use memcpy or memmove. Memcpy and memmove

The function is basically the same, but when the source string and target string have overlap, memmove can be handled correctly, but memcpy won't.

In addition, the difference between char * strcpy () and memcpy () is that when the source and target addresses overlap, the former is undefined,

It depends on the compiler. The latter creates a copy and can be implemented correctly. The former is not necessarily

 

In addition to the copy length of the source string and destination string,

Strcpy and sprintf are not taken into account, so when using it, it is likely that the memory of the destination string is smaller than the source

The memory size of the string, which will cause overflow and program crash due to memory exceptions.

 

At this time, we can consider using the copy operation function with a length limit, that is, strncpy and _ snprintf.

Strncpy: copy characters of one string to another.

Function prototype:

Char * strncpy (
Char * strdest,
Const char * strsource,
Size_t count
);

_ Snprintf: writes formatted data to a string.

Function prototype:

Int _ snprintf (
Char * buffer,
Size_t count,
Const char * Format [,
Argument]...
);

Of course, using these two functions only ensures that some unnecessary overflow errors are controlled, but the copy is not guaranteed to be successful, just like

The above strcpy and memcpy analysis, so if you need to copy the string, try to consider using memcpy

 

Finally, we can see that the sprintf and _ snprintf functions appear simultaneously in the copy and merge string operations.

The function of copying and merging characters is available. Format multiple source strings and then merge them into the destination string. If the destination string is

Null. In this case, a formatted copy operation is performed.

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.