Realization of strcpy function in C + + _c language

Source: Internet
Author: User

Let's take a look at an example first

char * strcpy (char * strdest,const char * strsrc) {
if (null==strdest) | |    (NULL==STRSRC)) 
Throw "Invalid argument (s)";
char * strdestcopy = strdest;
while ((*strdestcopy++ = *strsrc++)!= ' ");
return strdest;

I suddenly thought of a question I had done before

The prototype of the known strcpy function is:
char * strcpy (char * strdest,const char * strsrc);
1. Do not call library function, implement strcpy function.
2. Explain why you want to return char *.

1.STRCPY Implementation Code

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


        if (strdest==null) | | (Strsrc==null)) [1]

            throw "Invalid argument (s)";//[2]

        char * strdestcopy=strdest;//[3] While

        ((*strdest++=*strsrc++)! = '/0 '); [4] return



The wrong approach:

(A) Do not check the effectiveness of the pointer, indicating that the answer does not pay attention to the robustness of the code.
(B) Use when checking the validity of the pointer ((!strdest) | | (!STRSRC)) OR (!) ( STRDEST&&STRSRC), explains that the answer is not deeply aware of the implicit conversion of the type in C language. In this case, the conversion of char * to bool is a type implicit conversion, which, although flexible, is more likely to lead to increased error probability and higher maintenance costs. So C + + specifically adds bool, true, and false three keywords to provide a more secure conditional expression.
(C) Use when checking the validity of pointers ((strdest==0) | | (strsrc==0)), stating that the answer is not aware of the benefits of using constants. The direct use of literal constants, such as 0 in this example, reduces the maintainability of the program. 0 Although simple, but the program may appear many of the inspection of the pointer, in the event of a clerical error, the compiler can not find that the generated program contains logic errors, it is difficult to exclude. Instead of using null instead of 0, the compiler checks if a spelling error occurs.

(A) return a new string ("Invalid argument (s)"), stating that the answer is not at all aware of the purpose of the returned value, and that he is not alert to memory leaks. It is very dangerous to return the memory allocated in a function from a function, and he throws the obligation to free memory to an unwitting caller, which in most cases does not release memory, causing a memory leak.
(B) return 0, indicating that the answer has not mastered the abnormal mechanism. It is possible for the caller to forget to check the return value, and the caller may not be able to check the return value (see subsequent chain expressions). The illusion that the return value has the dual function of returning the correct value and the exception value is often the result of failure of both functions. The return value should be thrown in place of an exception, which reduces the burden on the caller, makes the error not ignored, and enhances the maintainability of the program.

(A) Forget to save the original strdest value, indicating that the answer is not closely logical.
(A) The loop is written while (*strdest++=*strsrc++);, with [1] (B).
(B) The loop is written as a while (*strsrc!= '/0 ') *strdest++=*strsrc++, stating that the examination by the answering person for the boundary condition is inadequate. After the loop body is finished, the end of the strdest string is not correctly appended with '/0 '.

    2. Returning the original value of Strdest enables the function to support chained expressions, adding "added value" to the function. Functions of the same function, if can reasonably improve the usability, natural is more ideal.
    chain expressions in the form of:
        int Ilength=strlen (strcpy stra, STRB));
    Another example:
        char * stra=strcpy (new CHAR[10],STRB); The
    returns the original value of STRSRC is incorrect. First, the source string is definitely known, and it doesn't make sense to return it. Second, you cannot support an expression in the form of an example. Thirdly, in order to protect the source string, the formal parameter uses const to qualify the contents of STRSRC, and the const char * is returned as char *, the type does not match, and the error is compiled.

Related Article

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: 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.