// Strcpy usage: // 1. If the source length <target length ---> OK. // Copy the source string to the target string (including the ending character), and keep other bytes unchanged. // 2. If the source length is greater than the target length, an error is returned. // Copy the source string to the target string (including the terminator). However, the target string is too short and the subscript exceeded, abnormal strings will obviously cause exceptions during runtime // usage principle Summary: Ensure the source length during use <target length !! Void test_strcpy () {char strsource [] = "sample string"; char strdestshort [10]; char strdestlong [40]; int isource = sizeof (strsource ); int idestshort = sizeof (strdestshort); int idestlong = sizeof (strdestlong); // 1if (isource <idestlong) strcpy (strdestlong, strsource); STD :: cout <strdestlong <STD: Endl; // 2if (isource> idestshort) {// strcpy (strdestshort, strsource ); in this way, an exception will be reported // it will be handled in the most traditional way. Of course, it is better to use the standard library function size_t L = sizeof (strdestshort); For (size_t I (0 ); I <L-1; ++ I) strdestshort [I] = strsource [I]; strdestshort [L-1] = 0;} STD :: cout <strdestshort <STD: Endl;} // strcpy_s usage: // if the source length is <target length sizeof ([min, Max]) ---> OK, however, when the length of a custom copy string exceeds the target length, a running exception may occur because the subscript is out of the range. // if the source length is greater than the target length, sizeof may run abnormally, // (it may be that the compiler checks the length of the two before copying. If the source string is long, an alarm is triggered and the system exits. Therefore, sizeof (strlen (strdest) = 0) is used later) statement is invalid) // usage principle Summary: // 1. ensure: Source length <target length // 2. the length cannot exceed szdest. We recommend that you use sizeof (szdest) void test_strcpy_s () {char strsource [] = "sample string"; char strdestshort [10]; char strdestlong [40]; int isource = sizeof (strsource); int idestshort = sizeof (strdestshort); int idestlong = sizeof (strdestlong); // 1if (isource <idestlong) limit (strdestlong, idestlong, strsource); STD: cout <strdestlong <STD: Endl; // 2if (isource> idestshort) strcpy_s (strdestshort, idestshort, strsource ); // This will report an exception STD: cout <strdestshort <STD: Endl ;}