Page 1 of 1

g++: deprecated conversion from string constant to char*

Posted: Tue Jul 15, 2008 2:58 pm
by Adek336
hello i found a new g++ feature that was absent in 2005 when I last coded in c++

Code: Select all

void eloszka(char *elo) { }
...
eloszka("ziom");
generates a warning: deprecated conversion from string constant to char*
which can be treated with casting or

Code: Select all

void eloszka(const char elo[]) { }
eloszka("ziom");
I'm probably missing some basic stuff here but why did the maintainers of g++ decide that a string constant was const char [] and not char* and that passing a string constant as a parameter which should be char * is wrong?

that "const" part is rather straight-forward: the string messages should not be changed as they're constants not buffers for computation. but why char[] like a table? tables and ptrs in C are rather related, isn't it so in C++ as well?

Re: g++: deprecated conversion from string constant to char*

Posted: Tue Jul 15, 2008 5:43 pm
by pcmattman
const char [] and not char*
Did you ever think to try const char* ? String literals are immutable, so they'll always be const char*. If you're using C++ you can use const_cast, though, but I don't suggest it.

Re: g++: deprecated conversion from string constant to char*

Posted: Tue Jul 15, 2008 6:50 pm
by Adek336
Did you ever think to try const char* ?
omg it works with const char *! so after all strings are still treated as pointers-to-char. as for const_cast, yes, exactly, there's no reason to follow the warnings if that means just putting const_casts everywhere where "const" is missing or unneeded.

Re: g++: deprecated conversion from string constant to char*

Posted: Fri Jul 18, 2008 3:56 am
by JamesM
there's no reason to follow the warnings if that means just putting const_casts everywhere where "const" is missing or unneeded.
Please tell me I didn't just read what I thought I read...

Re: g++: deprecated conversion from string constant to char*

Posted: Fri Jul 18, 2008 4:47 am
by Combuster
OMG save us *hides*

Re: g++: deprecated conversion from string constant to char*

Posted: Fri Jul 18, 2008 5:41 am
by AJ
Why not just refer to everything as a void* and just cast when necessary for mathematical operations :twisted:

Re: g++: deprecated conversion from string constant to char*

Posted: Fri Jul 18, 2008 9:54 am
by Adek336
JamesM wrote:
there's no reason to follow the warnings if that means just putting const_casts everywhere where "const" is missing or unneeded.
Please tell me I didn't just read what I thought I read...
Am I missing something? I said that abusing const_casts is a bad thing. Am I not correct?