My understanding is: if the function is to modify the pointer value represented by input parameters, it is safer to add Const. If the function is to modify the content of the input character array, const cannot be added.
Test code:
# Include <stdio. h> # include <string. h> void encode (char ** p_array); void test _ forward (); void encode (const char ** pp); void test _ parameter_with_qualifier_const_is_to_modify_pointer_value (); int main () {test _ parameter_no_qualifier_const_is_to_m Odify_chararray_contents (); test _ expect (); Return 0;} void expect (char ** p_array) {// if the function parameter format is const char ** p_array, the following line is followed by a compilation error: // error c2440: "initialization": cannot be converted from "const char *" to "char *" char * array = * p_array; * array ++ = 'C'; * array ++ = 'H'; * array ++ = 'I'; * array ++ = 'J '; * array ++ = 'K'; * Array = '\ 0'; // or, change the values of each element in the array in the following way. // If the function parameter format is const char ** p_array, the following four lines will encounter compilation errors: // error c3892: "p_array": the constant cannot be assigned a value. // (* p_array) [0] = 'C'; // (* p_array) [1] = 'H'; // (* p_array) [2] = 'I '; // (* p_array) [3] = 'J'; // (* p_array) [4] = 'K'; // (* p_array) [5] = '\ 0'; // (* p_array) ++;} void test _ parameter_no_qualifier_const_is_to_modify_chararray_contents () {char array [10] = {'A ', 'E', 'F', '\ 0', 'x', 'y', 'z',' \ 0 '}; printf ("% s \ r \ n ", Array); // AEF char * p_array = array; parameter_no_qualifier_const_is_to_modify_chararray_contents (& p_array); printf ("% s \ r \ n", array ); // chijk} void parameter_with_qualifier_const_is_to_modify_pointer_value (const char ** pp) {// The type of PP is const char **, that is, char const **, which is very large. Because function parameters are passed by value, internal modification does not affect the original value. // * The type of PP is const char *, that is, char const *, which is very large. The function can be indirectly modified. // ** The type of PP is const char, that is, char const and constant. The compiler prohibits the modification of its value. (* PP) ++; // ** pp = 'q'; // error c3892: "PP": constant assignment not allowed} void test _ parameter_with_qualifier_const_is_to_modify_pointer_value () {char * P = "prop"; printf ("% s \ r \ n", P); // outputs/error c2664: "parameter_use_qualifier_const_is_to_modify_pointer_value ": parameter 1 cannot be converted from "char **" to "const char **" // convert (& P); // The compiled C-style forced type conversion parameter_with_qualifier_con St_is_to_modify_pointer_value (const char **) & P); printf ("% s \ r \ n", P ); // XY char * P2 = "bytes"; printf ("% s \ r \ n", P2 ); // required // The equivalent and correct C ++ style is used for forced type conversion: const_cast parameter_with_qualifier_const_is_to_modify_pointer_value (const_cast <const char **> (& p2 )); printf ("% s \ r \ n", P2); // XY // char * P3 = "alias"; // error c2440: "static_cast ": cannot be converted from "char **" to "const char **" // parameter_with_qualifier_cons T_is_to_modify_pointer_value (static_cast <const char **> (& P3); // char * P4 = "Cola"; // error c2680: "const char **": the target type of dynamic_cast is invalid. // parameter_with_qualifier_const_is_to_modify_pointer_value (dynamic_cast <const char **> (& P4); // char * P5 = "Warn"; // error c2440: "reinterpret_cast": cannot be converted from "char **" to "const char **" // parameter_with_qualifier_const_is_to_modify_pointer_value (reinterpret_cast <Co NST char **> (& P5); // if it is Char () [] rather than char *. So ,... Char P6 [] = "dxy"; printf ("% s \ r \ n", p6); // dxy // error c2664: "parameter_with_qualifier_const_is_to_modify_pointer_value ": parameter 1 cannot be converted from "char (*) [4]" to "const char **" // parameter_with_qualifier_const_is_to_modify_pointer_value (& P6); // when the input parameter is an array, you also want to modify the pointer value, so it won't work (the value of the array name cannot be modified ). // The compiled C-style forced type conversion parameter_with_qualifier_const_is_to_modify_pointer_value (const char **) (& P6); printf ("% s \ r \ n ", p6); // exy (? Because: 'E' = 'D' + 1) // the correct method is: char * P6 _ = P6; printf ("% s \ r \ n ", p6 _); // exy parameter_with_qualifier_const_is_to_modify_pointer_value (const char **) (& P6 _); printf ("% s \ r \ n ", p6 _); // XY // char P7 [] = "dxy"; // error c2440: "const_cast": cannot start from "char (*) [4] "to" const char ** "// parameter_with_qualifier_const_is_to_modify_pointer_value (const_cast <const char **> (& P7); // char P8 [] =" dxy "; // error C 2440: "static_cast": cannot start from "char (*) [4] "to" const char ** "// parameter_with_qualifier_const_is_to_modify_pointer_value (static_cast <const char **> (& P8); // char P9 [] =" dxy "; // error c2680: "const char **": The target type of dynamic_cast is invalid // parameter_with_qualifier_const_is_to_modify_pointer_value (dynamic_cast <const char **> (& P9 )); char P10 [] = "dxy"; printf ("% s \ n", P10 ); // dxy // The C ++ style that can be compiled but fails to be forcibly converted is Rei. Nterpret_cast encode (reinterpret_cast <const char **> (& p10); printf ("% s \ n", P10); // exy (? Because: 'E' = 'D' + 1) char * P10 _ = P10; printf ("% s \ n", P10 _); // exy parameter_with_qualifier_const_is_to_modify_pointer_value (const_cast <const char **> (& P10 _); printf ("% s \ n", P10 _);/XY}