Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const char** parameter warning about char** argument

Tags:

c

char

constants

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?

like image 830
James Morris Avatar asked Apr 10 '26 04:04

James Morris


1 Answers

Make the conversion explicit with a cast and the compiler will be happy:

process_array_of_strings((const char**) foo);

In these cases you have to explicitly say that you know what you're doing.

like image 85
effeffe Avatar answered Apr 12 '26 20:04

effeffe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!