Implementation and difference of strcpy and memcpy functions in C Language

Source: Internet
Author: User

Implementation and difference of strcpy and memcpy functions in C Language

What are the differences between strcpy and memcpy functions in C language? Next, let's give a brief introduction. I hope you can give more comments and welcome to comment and correct the mistakes.

6.2 string and array

Strings are generally stored as character arrays, for example, the following str definition:

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.

6.2.1 strcpy and memcpy Functions

Strcpy and memcpy are both standard C library functions, which have the following features.

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.

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

Interview Example 5: implement the strcpy function by programming.

Test site: Implementation of string replication.

Frequency:★★★★

It is known that the prototype of the strcpy function is:

Char * strcpy (char * strDest, const char * strSrc );

The requirements are as follows.

(1) Implement the strcpy function without calling the database function;

(2) Explain why char * is returned *.

Analysis

The program code is as follows:

1# Include
2
3Char * strcpy (char * strDest, const char * strSrc) // implementation
Replication from strSrc to strDest
4{
5If (strDest = NULL) | (strSrc = NULL) //
Validity of strDest and strSrc
6{
7Return NULL;
8}
9Char * strDestCopy = strDest;// Save the first address of the target string
10While (* strDest ++ = * strSrc ++ )! = '\ 0'); //
Copy the content of the strSrc string to strDest.
11
12Return strDestCopy;
13}
14
15Int getStrLen (const char * strSrc)// Obtain the strSrc String Length
16{
17Int len = 0; // Save the length
18While (* strSrc ++! = '\ 0 ')// Loop until the terminator '\ 0' is met
19{
20Len ++;
21}
22
23Return len;
24};
25
26Int main ()
27{
28Char strSrc [] = "Hello World! ";// Source string to be copied
29Char strDest [20];// Target character array to be copied
30Int len = 0;// Save the object
The length of the string in the string array.
31
32Len = getStrLen (strcpy (strDest, strSrc); // chain
Expression. Copy the expression first and then calculate the length.
33Printf ("strDest: % s \ n", strDest );
34Printf ("Length of strDest: % d \ n", len );
35
36Return 0;
37}

(1) Implementation of strcpy functions.

Code 5th ~ Row 3 checks whether the passed strDest and strSrc parameters are NULL. If yes, return NULL.

Line 3 of the Code saves the strDest value to the strDestCopy pointer.

Line 4 of the Code cyclically moves the strSrc and strDest pointers and continuously copies the strSrc memory value to the strDest memory.

Because the strDest pointer value has been saved, only the strDestCopy value is returned here, And the strDest value is returned after the function is called.

(2) The strcpy function returns the char * type to use a chained expression. First, call strcpy to make the strDest pointer copy the memory data of strSrc, and then call the getStrLen function to obtain the length of the strDest string. In this way, the call is convenient, and the program structure is concise and clear. The output result of the program is as follows:

StrDest: Hello World!
Length of strDest: 12

Interview Example 6: Program and implement the memcpy function.

Test site: Implementation of memory replication.

Frequency:★★★★

Answer

The program code is as follows:

1# Include
2# Include
3
4Void * memcpy2 (void * memTo, const void * memFrom, size_t size)
5{
6Assert (memTo! = NULL) & (memFrom! = NULL); // memTo and memFrom must be valid
7Char * tempFrom = (char *) memFrom;// Save the first memFrom address
8Char * tempTo = (char *) memTo;// Save the first memTo address
9
10While (size --> 0)// Loop size times, copy the value of memFrom to memTo
11* TempTo ++ = * tempFrom ++;
12
13Return memTo;
14}
15
16Int main ()
17{
18Char strSrc [] = "Hello World! ";// The character array to be copied
19Char strDest [20];// Target character array
20
21Memcpy2 (strDest, strSrc, 4 );// Copy the first four characters of strSrc to strDest.
22StrDest [4] = '\ 0 ';// Assign the 5th elements of strDest to the terminator '\ 0'
23Printf ("strDest: % s \ n", strDest );
24
25Return 0;
26}

The implementation of memcpy is as follows.

Unlike strcpy, memcpy uses the parameter size to determine the number of characters to be copied (strcpy ends with the Terminator "\ 0 ). Since the main program only copies the first four characters of strSrc (line 1 of code), the program output is as follows:

StrDest: Hell

Interview Example 7: What is the difference between strcpy and memcpy.

Test site: the difference between string replication and memory replication.

Frequency:★★★★

Analysis

Strcpy and memcpy have the following three differences.

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

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.

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

The above is my summary of experience. I hope you will reply enthusiastically!

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.