Why my compiler(GCC) doesnt implicitly cast from char**
to const char**
?
Thie following code:
#include <iostream>
void print(const char** thing) {
std::cout << thing[0] << std::endl;
}
int main(int argc, char** argv) {
print(argv);
}
Gives the following error:
oi.cpp: In function ‘int main(int, char**)’:
oi.cpp:8:12: error: invalid conversion from ‘char**’ to ‘const char**’ [-fpermissive]
oi.cpp:3:6: error: initializing argument 1 of ‘void print(const char**)’ [-fpermissive]
Such a conversion would allow you to put a const char*
into your array of char*
, which would be unsafe. In print
you could do:
thing[0] = "abc";
Now argv[0]
would point to a string literal that cannot be modified, while main
expects it to be non-const (char*
). So for type safety this conversion is not allowed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With