This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Problem with "warning: deprecated conversion from string constant to âchar*â"
Jens Rehsack <rehsack@googlemail.com> writes:
> 2010/10/12 Ian Lance Taylor <iant@google.com>:
>> Jens Rehsack <rehsack@googlemail.com> writes:
>>
>>> I currently write some c++ code using libConfuse
>>> (http://savannah.nongnu.org/projects/confuse/).
>>> During initialisation of the required cfg_opt_t structures I get a
>>> (very) lot of warnings like:
>>> c2.cpp:11: warning: deprecated conversion from string constant to âchar*'
>>>
>>> I attached a small example to demonstrate the problem. It's not
>>> trivial - changing the "name" member
>>> of struct demo being const, free failes (and is invalid, because
>>> there're demo_opt instances with
>>> modifyable names).
>>>
>>> Any suggestions welcome.
>>> 1) Killing the warning with -Wno-... is not an option
>>> 2) Remove the finding using const_cast<char *>() is the same as (1)
>>> 3) initializing the struct using strdup is neither reasonable
>>
>> I'm not sure what you want us to suggest. ÂYou say that the names are
>> modifiable.
>
> Where do I say that? I apologize saying this (must be happened because
> of my not so good english).
"changing the "name" member of struct demo being const, free failes (and
is invalid, because there're demo_opt instances with modifyable names)."
I apologize if I misread that sentence.
>> ÂString constants are not modifiable. ÂIf the strings are
>> indeed modified, and you pass in a string constant, then your program is
>> going to crash when the library tries to modify the strings. ÂThe gcc
>> warning is helpfully pointing that out.
>
> Correct. And if the strings are not modifiable? (I thought I provided an
> example which shows, that they neither are assumed being modifiable
> nor being modified).
>
>> This is not a gcc issue. ÂIt's a C++ language issue. ÂIf you need a
>> modifiable string, then you must not use a string constant.
>
> How can this being expressed without declaring dozen of helper
> variables?
I am not sure that I understand what you are asking. Can you provide a
short, standalone, bit of code showing the problem?
Is there any reason to think that this is a gcc issue rather than a C++
issue?
Ian