Implementation of strcpy function

Source: Internet
Author: User

The prototype of the known strcpy function is a char *strcpy (char *strdest, const char *STRSRC);

Where strdest is the destination string, Strsrc is the source string.

(1) Do not call the C++/C string library function, write the function

strcpy Char *strcpy (char *strdest, const char *STRSRC);

{

ASSERT ((strdest!=null) && (strsrc!=null)); 2 min

char *address = strdest; 2 min

while ((*strdest++ = * strsrc++)! = ' + ')//2 min

NULL;

return address; 2 min

}

(2) strcpy can copy the contents of STRSRC to strdest, why do you want the return value of char * type?

Answer: In order to implement the chain expression. 2 min

For example int length = strlen (strcpy (strdest, "Hello World"));

--------------------------------------------------------------------------------------------------------------- -----------------

See the word: Chain expression, have not heard before, so went to Baidu a bit, found that some people explain the figure is clear:

--------------------------------------------------------------------------------------------------------------- -----------------http://topic.csdn.net/t/20061123/21/5180993.html 1,

is convenient, otherwise it will be written:

Char strdest[12];

strcpy (strdest, "Hello World");

int length = strlen (strdest);

What chain is a series of written down meaning ...

--------------------------------------------------------------------------------------------------------------- -----------------

In fact, plainly, is if the above string copy function strcopy return value is void, then, the above sentence:

int length = strlen (strcpy (strdest, "Hello World"));

It is like the answer of the above, written several sentences:

Char strdest[12];

strcpy (strdest, "Hello World");

int length = strlen (strdest);

This method of directly returning char * is designed for the convenience of later function callers. You don't have to bother with this. Using the above methods, you can directly use the copy of the Dest string. This is a convenient way to do this, it looks like chain-chained, so called chained expression

That's all. hehe.

About chaining expressions

The prototype of the known strcpy function is a char *strcpy (char *strdest, const char *STRSRC);

Where strdest is the destination string, Strsrc is the source string.

(1) Do not call the C++/C string library function, write the function

strcpy Char *strcpy (char *strdest, const char *STRSRC);

{

ASSERT ((strdest!=null) && (strsrc!=null)); 2 min

char *address = strdest; 2 min

while ((*strdest++ = * strsrc++)! = ' + ')//2 min

NULL;

return address; 2 min

}

(2) strcpy can copy the contents of STRSRC to strdest, why do you want the return value of char * type?

Answer: In order to implement the chain expression. 2 min

For example int length = strlen (strcpy (strdest, "Hello World"));

--------------------------------------------------------------------------------------------------------------- -----------------

See the word: Chain expression, have not heard before, so went to Baidu a bit, found that some people explain the figure is clear:

--------------------------------------------------------------------------------------------------------------- -----------------http://topic.csdn.net/t/20061123/21/5180993.html 1,

is convenient, otherwise it will be written:

Char strdest[12];

strcpy (strdest, "Hello World");

int length = strlen (strdest);

What chain is a series of written down meaning ...

--------------------------------------------------------------------------------------------------------------- -----------------

In fact, plainly, is if the above string copy function strcopy return value is void, then, the above sentence:

int length = strlen (strcpy (strdest, "Hello World"));

It is like the answer of the above, written several sentences:

Char strdest[12];

strcpy (strdest, "Hello World");

int length = strlen (strdest);

This method of directly returning char * is designed for the convenience of later function callers. You don't have to bother with this. Using the above methods, you can directly use the copy of the Dest string. This is a convenient way to do this, it looks like chain-chained, so called chained expression

That's all.

Implementation of strcpy 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.