Home>

strncpy This can specify the length of the copied characters,Specify the source address,target address,And the length of the characters to be copied;strcpy can only pass two parameters,Specify only the start address and destination address of the copy,Then copy it as a whole;

Let's 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++)!="\ 0");
Return strdest;
}

Suddenly thought of a test question I did before

topic:

The prototype of the known strcpy function is:

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

1. Do not call library functions,Implement the strcpy function.

2. Explain why char * is returned.

Commentary:

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 strdestcopy;
    }

Wrong approach:

[1]

(a) does not check the validity of the pointer,Explain that the respondents did not pay attention to the robustness of the code.

(b) Use ((! strdest) || (! strsrc)) or (! (strdest&&strsrc)) when checking the validity of the pointer, indicating that the respondent does not have a deep understanding of the implicit conversion of types in C language.In this example, the conversion from char * to bool is implicit type conversion.Although this feature is flexible,However, it is more likely to increase the probability of errors and increase maintenance costs.So C++ specifically added three keywords:bool, true, and false to provide safer conditional expressions.

(c) Use ((strdest == 0) || (strsrc == 0)) when checking the validity of the pointer, indicating that the respondent does not know the benefits of using constants.Using literal constants directly (such as 0 in this example) will reduce program maintainability.0 Although simple,But there may be many checks on pointers in the program,In case of typos,The compiler cannot find out,The generated program contains logic errors,It's hard to rule out.And use null instead of 0, if there is a typo,The compiler will check it out.

[2]

(a) return new string ("invalid argument (s)") ;, indicating that the respondent did not know the purpose of the return value at all,And he was not wary of memory leaks.It is very dangerous to return the memory allocated in the function body from a function.He tossed the obligation to release memory to an unsuspecting caller,In most cases,The caller does not release the memory,This causes a memory leak.

(b) return 0 ;, indicating that the respondent did not grasp the abnormal mechanism.The caller may forget to check the return value,The caller may also not be able to check the return value (see chained expressions below). The delusion that the return value shoulders the dual function of returning the correct value and the abnormal value,As a result, both functions fail.Throw exceptions instead of return values,This can reduce the burden on the caller, prevent errors from being ignored, and enhance the maintainability of the program.

[3]

(a) Forgot to save the original strdest value, indicating that the respondent's logical thinking was not strict.

[4]

(a) The loop is written as while (* strdest ++=* strsrc++);same as [1] (b).

(b) The loop is written as while (* strsrc!="/0") * strdest ++=* strsrc++ ;, indicating that the respondent's check on the boundary conditions is not good.After the loop body ends,The strdest string is not properly appended with "/0" at the end.

2. Returning the original value of strdest enables the function to support chained expressions,Added "added value" to functions. Functions with the same function,If you can reasonably increase availability,Naturally more ideal.

The form of a chained expression is:

int ilength=strlen (strcpy (stra, strb));

Another example:

char * stra=strcpy (new char [10], strb);

It is wrong to return the original value of strsrc.First, the source string must be known,It doesn't make sense to return it.Second, expressions like the second example cannot be supported.Third, in order to protect the source string,Formal parameters use const to limit what strsrc refers to,Return const char * as char *, the type does not match,Compile error.

c
  • Previous Android power information view (electricity, temperature, voltage) example code
  • Next Jsp + mysql database operation common method instance summary