During compilation of a call to the following function:
char* process_array_of_strings(const char** strings);
GCC complains when a char** is passed as argument:
note: expected ‘const char **’ but argument is of type ‘char **’
While the function does not alter the characters (hence the const) it does duplicate the array of pointers in order to modify the character pointers themselves, so constant pointers are definitely undesirable here.
Compilation succeeds and the program appears to work. So how is the programmer supposed to handle this warning?
This is why
char **is not automatically converted toconst char **in C++, and why the C compiler issues a warning while allowing it.From your description of what
process_array_of_strings()does, it could just as well takeconst char * const *because it modifies neither the pointers nor the characters (but duplicates the pointers elsewhere). In that case, the above scenario would not be possible, and compiler theoretically could allow you to convertchar **toconst char * const *automatically without warnings, but that’s not how the language is defined.So the answer is obviously that you need a cast (explicit). I’ve written up this expanation so that you can fully understand why the warning appears, which is important when you decide to silence one.