In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "strcpy function implementation and matters needing attention", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "strcpy function implementation and matters needing attention"!
The strcpy writing method of the written examination questions with the highest frequency
Title:
The known prototype of the strcpy function is:
Char * strcpy (char * strDest,const char * strSrc)
Request:
1. Implement the strcpy function without calling the library function.
two。 Explain why char * is returned.
Commentary:
The implementation code of 1.strcpy
Char * strcpy (char * strDest,const char * strSrc) {char * strDestCopy=strDest; / / [3] if ((strDest==NULL) | | (strSrc==NULL)) / / [1] throw "Invalid argument (s)"; / / [2] while ((* strDest++=*strSrc++)! ='\ 0'); / / [4] return strDestCopy;}
The wrong approach:
[1]
(a) do not check the validity of the pointer, indicating that the respondent does 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 case, the conversion from char * to bool is an implicit type conversion, which is flexible but more likely to increase the probability of error and the cost of maintenance. So C++ specially added three keywords bool, true and false to provide more secure conditional expressions.
(C) use ((strDest==0) | | (strSrc==0)) when checking the validity of pointers, indicating that the respondent is not aware of the benefits of using constants. The direct use of literal constants, such as 0 in this case, reduces the maintainability of the program. Although 0 is simple, there may be many checks for pointers in the program. In case of a pen error, the compiler cannot find that the generated program contains logic errors, which is difficult to eliminate. Using NULL instead of 0, if there is a spelling error, the compiler will check it out.
[2]
(a) return new string ("Invalid argument (s)"); which means that the respondent has no idea what the return value is for, and he is not wary of memory leaks. It is very dangerous to return the memory allocated in the function body from the function. He throws the obligation of releasing memory to the unwitting caller, who in most cases does not release memory, which leads to memory leak.
(B) return 0 position, indicating that the respondent did not grasp the abnormal mechanism. The caller may forget to check the return value, and the caller may not be able to check the return value (see chained expression below). The delusion of making the return value shoulder the dual function of returning correct values and outliers often results in the failure of both functions. The return value should be replaced by throwing an exception, which can lighten 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 is not strict.
[4]
(a) write the loop as while (* strDest++=*strSrc++);, as well as [1] (B).
(B) the loop is written as while (* strSrcised conditions'\ 0') * strDest++=*strSrc++;, to show that the interviewees did not check the boundary conditions properly. At the end of the loop body, the'\ 0' is not correctly added to the end of the strDest string.
two。 Returning the original value of strDest enables the function to support chained expressions, increasing the "added value" of the function. The function of the same function, if it can reasonably improve the availability, is naturally more ideal.
The form of a chain expression is as follows:
Int iLength=strlen (strcpy (strA,strB))
Another example is:
Char * strA=strcpy (new char [10], strB)
It is wrong to return the original value of strSrc. For one thing, the source string must be known, and there is no point in returning it. Second, the expression like the second example cannot be supported. Third, in order to protect the source string, the formal parameter uses const to define what strSrc refers to, and returns const char * as char *. The type does not match and the compilation error is reported.
In the above statement, the loop statement while ((* strDest++=*strSrc++)! ='\ 0') is difficult to understand and can be understood as the following.
The first kind:
While (1) {char temp; temp=*strDest=*strSrc; strDest++; strSrc++; if ('\ 0'==temp) break;}
The second kind:
While (* strSr _ strcages\\ 0') {* strDest=*strSrc; strDest++; strSrc++;} * strDest=*strSrc
Personal opinion: throw usage obviously does not work, if you want to judge to add # include
If the value of the expression is false, the entire program exits with an error message. If the value of the expression is true, continue to execute the following statement.
You need to include the header file assert.h before using this macro
# include # include char * strcpy (char * strDest, const char * strSrc) {assert ((strDestrated null) & & (strSrc! = NULL)); char * strDestCopy=strDest; while ((* strDest++=*strSrc++)! ='\ 0'); return strDestCopy;} void main () {char a [20], c [] = "i am teacher!"; strcpy (aMagna); cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.