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

Programming, for all ages and all languages.
Post Reply
User avatar
Adek336
Member
Member
Posts: 129
Joined: Thu May 12, 2005 11:00 pm
Location: Kabaty, Warszawa
Contact:

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

Post 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?
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

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

Post 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.
User avatar
Adek336
Member
Member
Posts: 129
Joined: Thu May 12, 2005 11:00 pm
Location: Kabaty, Warszawa
Contact:

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

Post 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.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

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

Post 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...
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

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

Post by Combuster »

OMG save us *hides*
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

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

Post by AJ »

Why not just refer to everything as a void* and just cast when necessary for mathematical operations :twisted:
User avatar
Adek336
Member
Member
Posts: 129
Joined: Thu May 12, 2005 11:00 pm
Location: Kabaty, Warszawa
Contact:

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

Post 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?
Post Reply