Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: invalid conversion from 'char**' to 'const char**' [duplicate]

I've got a function that requires const some_type** as an argument (some_type is a struct, and the function needs a pointer to an array of this type). I declared a local variable of type some_type*, and initialized it. Then I call the function as f(&some_array), and the compiler (gcc) says:

error: invalid conversion from ‘some_type**’ to ‘const some_type**’

What's the problem here? Why can't I convert a variable to const?

like image 692
petersohn Avatar asked Nov 20 '22 14:11

petersohn


2 Answers

See: Why can't I pass a char ** to a function which expects a const char **? from the comp.lang.c FAQ.

like image 78
jamesdlin Avatar answered Dec 21 '22 11:12

jamesdlin


You have a few options to get around what jamesdlin outlined in his answer.

You could use an intermediate variable.

some_type const* const_some_array = some_array;
f(&const_some_array);

You could change the parameters of f.

void f(some_type const* const* some_array);
like image 30
Matthew T. Staebler Avatar answered Dec 21 '22 10:12

Matthew T. Staebler