Optimal implementation of strcpy functions

Source: Internet
Author: User
Strcpy function provided by Microsoft: (incomplete, 4 points)

Char * strcpy (char * dest, const char * src) {char * tmp = dest; while (* dest ++ = * src ++ )! = '\ 0') return tmp ;}

The most complete strcpy function: (recommended, 10 points)

Char * strcpy (char * dest, const char * src) // add the source string to const, indicating that it cannot be modified as an input parameter {if (dest = src) // considering that the source string and target string overlap, that is, the address is the same {return dest;} assert (dest! = NULL) & (src! = NULL); // add a non-empty address assertion char * tmp = dest to the source address and destination address; // back up the first address of the destination string, because the subsequent operation will modify the dest value while (* dest ++ = * src ++ )! = '\ 0'); return tmp; // return the destination address to obtain the function return value for chained operations ;}

Recommendation reason: (view score points)

// Get 2 points void strcpy (char * dest, char * src) {while (* dest ++ = * src ++ )! = '\ 0');} // Get 4 points void strcpy (char * dest, const char * src) {// add the source string to const, indicating that it is an input parameter, + 2 points while (* dest ++ = * src ++ )! = '\ 0');} // Get 7 points void strcpy (char * dest, const char * src) {// add non-0 assertions to the source address and destination address, add assert (dest! = NULL) & (src! = NULL); while (* dest ++ = * src ++ )! = '\ 0');} // Get 9 points // in order to achieve chained operation, return the destination address and add 2 points! Char * strcpy (char * dest, const char * src) {assert (dest! = NULL) & (src! = NULL); char * tmp = dest; while (* dest ++ = * src ++ )! = '\ 0'); return tmp;} // 10 points. Basically, all the situations are taken into account. // if there are overlapping regions in the source object, add 1 point! Char * strcpy (char * dest, const char * src) {if (dest = src) {return dest;} assert (dest! = NULL) & (src! = NULL); char * tmp = dest; while (* dest ++ = * src ++ )! = '\ 0'); return tmp ;}

Assert summary


Understanding and use of assert "assertions:

A.Assertion, that is, to make some assumptions, assuming that the expression is correct, if the program runs correctly, the assertion program is established; if the assertion expression is not true, the program will certainly run incorrectly, the entire program will exit. It is mostly used for Debug debugging and can quickly locate errors;

Assert (expression );

If the expression value is false, the entire program exits and an error message is output. If the expression value is true, continue executing the following statement.


B.Assert is a macro, not a function. To use this macro, you need to add the header file # include <assert. h>


C.Differences between assert and if:

If: execution continues if the condition is true or not.

Assert: The execution continues when the condition is set, and the execution stops when the condition is not set.


Supplement: error-prone points for strcpy functions



Optimal implementation of strcpy functions

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.