The realization and difference of strcpy and memcpy function in C language

Source: Internet
Author: User

What are the differences between the strcpy and memcpy functions in C language? Below with me to introduce a simple, I hope you give more comments, welcome comments to correct errors.

6.2 Strings and Arrays

Strings are typically stored in a character array, such as the following STR definition:

Char str[] = "123456";

Here str is an array of characters, it holds a string "123456", because the string also has a Terminator "" ", so the length of this array is 7 instead of 6.

6.2.1 strcpy function and memcpy function

strcpy and memcpy are standard C library functions, and they have the following characteristics.

strcpy provides a copy of the string. That is, strcpy is used only for string copying, and it replicates not only the string contents, but also the terminator of the string.

MEMCPY provides replication of general memory. That is, memcpy has no limitations on what needs to be replicated and is therefore more widely used.

Interview Example 5: Programmatic implementation of the strcpy function.

Test Center: The implementation of string replication.

Occurrence frequency: ★★★★

The prototype of the known strcpy function is:

char * strcpy (char * strdest,const char * strsrc);

Requirements are as follows.

(1) Do not call library function, implement strcpy function;

(2) explain why you want to return char *.

Analytical

The program code is as follows:

1#include
2
3char * strcpy (char * strdest, const char * STRSRC)//implemented
Copy of STRSRC to Strdest
4{
5if ((strdest = = NULL) | | (strsrc = = NULL)) Sentenced
The validity of parameters strdest and STRSRC
6{
7return NULL;
8}
9char *strdestcopy = strdest;Save the first address of the target string
10while ((*strdest++ = *strsrc++)! = ' + '); Put
Copy the contents of the STRSRC string to strdest
11
12return strdestcopy;
13}
14
15int Getstrlen (const char *STRSRC)Implementation gets the length of the STRSRC string
16{
17int len = 0; Save length
18while (*strsrc++! = ')Loop until you meet the Terminator
19{
20len++;
21st}
22
23return Len;
24};
25
26int main ()
27{
28Char strsrc[] = "Hello world!";The source string to be copied
29Char strdest[20];Array of destination characters to copy to
30int len = 0;Save the Project
The length of the string in the character array
31
32Len = Getstrlen (strcpy (strdest, strsrc)); Chain
expression, the length of the calculation is first copied
33printf ("Strdest:%s\n", strdest);
34printf ("Length of strdest:%d\n", Len);
35
36return 0;
37}

(1) Description of the implementation of the strcpy function.

The Code 5~ line 7th determines whether the parameters passed in strdest and strsrc are null, and if so, returns NULL.

Line 9th of the code saves the value of strdest to the strdestcopy pointer.

Line 10th of the code loops through the STRSRC and strdest two pointers and constantly replicates the value of STRSRC memory into strdest memory.

Since the value of the strdest pointer has been saved, only the value of the strdestcopy is returned, and the value of strdest is returned when the function call is complete.

(2) The reason why the strcpy function returns a char * type is to be able to use a chain expression. Call strcpy first to make the strdest pointer copy STRSRC's memory data, and then call the Getstrlen function to get the length of the strdest string. This is not only convenient to call, but also simple and concise program structure. The output of the program is as follows:

Strdest:hello world!
Length of Strdest:12

Interview Example 6: Programmatic implementation of the memcpy function.

Test Center: The implementation of memory replication.

Occurrence frequency: ★★★★

Answer

The program code looks like this:

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 effective.
7Char *tempfrom = (char *) memfrom;Save Memfrom First Address
8Char *tempto = (char *) memto;Save Memto First Address
9
10while (Size--> 0)Loop size, copy the value of Memfrom to Memto
11*tempto++ = *tempfrom++;
12
13return memto;
14}
15
16int main ()
17{
18Char strsrc[] = "Hello world!";Array of characters to be copied
19Char strdest[20];Destination character Array
20
21stMemcpy2 (Strdest, STRSRC, 4);Copy the first 4 characters of a strsrc into strdest
22STRDEST[4] = ' + ';Assign the 5th element of the strdest as the Terminator ' \ X '
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 how many characters to copy (strcpy encounters the end of the Terminator "" "). Since only the first 4 characters of STRSRC are copied in the main program (line 22nd of the Code), the program output is as follows:

Strdest:hell

Interview examples 7:strcpy and memcpy difference.

Test Center: The difference between a string copy and a memory copy.

Occurrence frequency: ★★★★

Analytical

strcpy and memcpy mainly have the following 3 aspects of the difference.

The contents of the copy are different. strcpy can only copy strings, while memcpy copies arbitrary content, such as character arrays, integers, structs, classes, and so on.

The methods for copying are different. strcpy does not need to specify a length, it encounters the string Terminator "\" and ends. memcpy determines the length of the copy according to its 3rd parameter.

Use different. It is common to use strcpy when copying strings, and memcpy when copying other types of data.

The above is my summary of experience, I hope everyone enthusiastically reply!

The realization and difference of strcpy and memcpy function in C language

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.