Hi Jens, On Tue, Oct 12, 2010 at 05:42:32PM +0200, Jens Rehsack wrote: > 2010/10/12 Ian Lance Taylor <iant@xxxxxxxxxx>: > > Jens Rehsack <rehsack@xxxxxxxxxxxxxx> 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). I think, what Ian wanted to say: The problem is in your code -- your code is NOT clean C++. What the code does, is to create non-constant pointers of type "char*" pointing to string-constants. That IS a problem in the code (The C++-Standard states "An ordinary string literal has type âarray of n const charâ" (Â2.13.4.1) -- and you can't have a non-constant pointer pointing to a constant array!). > > > Â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). Well, your code does not modify the string, that's right. However, it creates a non-constant pointer of type "char *" -- and that assums that the string would be modifiable (later on, the compiler will never be able to detect it when you try to change a character in the string!) Also the fact that you declare "static const struct demo_opt foo" as beeing const does not change this problem: you can use the pointers in "foo" in order to get write-access to the string constants: If you write somewhere in your main-function "foo[1].name[3] = 'c';" the compiler won't detect an error -- however, you have undefined behaviour as you try to change a string constant > > > 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 think, you can't. Maybe you could define two different structures "demo_opt_const" and "demo_opt" and then use "demo_opt_const" as static and pass it to "demo_init", and use in the struct "demo" the class "demo_opt", where you define: struct demo_opt { char *name; int flags; }; struct demo_opt_const { const char *name; int flags; }; That should work, I think. However, of course I have no idea how much you would have to change in your final code. Axel