Thread: deprecated conversion from string constant to char*

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    deprecated conversion from string constant to char*

    Hi,

    I have a problem. so i have this old c function that takes as an argument a char*. but my app is written in cpp so i used std::string to store my strings. to pass a char * to the function i tried :

    Code:
    char *input = new char[args.i.length() + 1];
    strcpy(input, args.i.c_str());
    and then
    Code:
    function (input);
    and
    function ( (char *)input);
    but i still get this warning message which i would like to fix:


    warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

    any suggestions ?

    baxy

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Where exactly do you get that warning?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    that line doesn't appear to be the problem. you'll usually get that warning when you do something like this:

    Code:
    char* myString = "foo!";
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Does the function actually modify the data pointed to by its char* argument, or is does not alter it at all? In the latter case I think this would be a situation where a const_cast would be acceptable.

    Code:
    function( const_cast < char* > ( args.i.c_str() ) );

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    222
    ok, so i'm lost, why did this work?

    Code:
    function( const_cast < char* > ( args.i.c_str() ) );

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by baxy View Post
    ok, so i'm lost, why did this work?

    Code:
    function( const_cast < char* > ( args.i.c_str() ) );
    Because function is not const-correct (assuming it does not modify the data pointed at by its argument).

    args.i.c_str() returns a pointer to a CONSTANT string (const char *) while your function is expecting a non-constant string (char *). Your cast is explicitly telling the compiler it's OK to pass a pointer to a constant in this instance.

    The better method would have been to code function() to take a parameter that is of type const char *, assuming it couldn't be modified to take a string.

    Again, this is all assuming that function() does not modify the string. If it does, then additional work is needed.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, this doesn't make any sense: the type of the argument to the function named function in post #1 in both cases is the same as the type of the argument after the const_cast to char* is applied. Hence, I do not see why the warning should disappear, unless you removed some other offending code that you have not shown us.

    As has been mentioned, the use of const_cast will be correct if the function's parameter is not const correct. If it does modify what the pointer points to, then you have undefined behaviour instead, in which case it might appear to work, but actually doesn't.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    If it does modify what the pointer points to, then you have undefined behaviour instead, in which case it might appear to work, but actually doesn't.
    Technically, a permitted result of undefined behaviour is that code which appears to work actually does. The problem is, that code is not guaranteed to work, and is permitted to work incorrectly, can work with one compiler but not another, etc etc.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 07-24-2011, 11:34 PM
  2. warning: deprecated conversion from string constant to char*
    By Albinoswordfish in forum C++ Programming
    Replies: 2
    Last Post: 12-23-2008, 10:24 AM
  3. Replies: 7
    Last Post: 09-24-2008, 03:48 AM
  4. conversion from string to char
    By erik_markman in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2003, 02:10 PM
  5. Replies: 5
    Last Post: 10-09-2002, 12:37 PM