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