const char * to char *

Discussion in 'C++' started by Steven C, Oct 9, 2003.

  1. Steven C

    Steven C Guest

    I have a 3rd party libary that takes a char * (not a const char *).

    if I have:

    string strSome;
    char *the3rdpartylib;

    how do I:
    the3rdpartylib = strSome.c_str();

    I have done:
    the3rdpartylib = (char *) strSome.c_str();

    which gets by the compiler messages but the lint program is complaining about
    never assign a const to a non const.

    I tried this:
    the3rdpartylib = static_cast <char *> (strSome.c_str());

    which gives compiler error message:
    can't convert from const char * to char *

    thanks
     
    Steven C, Oct 9, 2003
    #1
    1. Advertising

  2. Steven C

    Ron Natalie Guest

    "Steven C" <> wrote in message news:...

    > I have done:
    > the3rdpartylib = (char *) strSome.c_str();
    >
    > which gets by the compiler messages but the lint program is complaining about
    > never assign a const to a non const.


    And lint is right. If your third party lib changes the string, you have undefined behavior.
    Your best bet is to temporarily copy the string to some non-const place:

    vector<char> tmp(strSome.begin(), strSome.end());
    the3rdpartlib = &tmp[0];
    >
    > I tried this:
    > the3rdpartylib = static_cast <char *> (strSome.c_str());
    >
    > which gives compiler error message:
    > can't convert from const char * to char *


    static_cast can't remove const.

    const_cast<char*>(strSome.c_str())

    is the same as the C-style cast you did above.
     
    Ron Natalie, Oct 9, 2003
    #2
    1. Advertising

  3. "Steven C" <> wrote in message
    news:...
    > I have a 3rd party libary that takes a char * (not a const char *).
    >
    > if I have:
    >
    > string strSome;
    > char *the3rdpartylib;
    >
    > how do I:
    > the3rdpartylib = strSome.c_str();
    >
    > I have done:
    > the3rdpartylib = (char *) strSome.c_str();
    >
    > which gets by the compiler messages but the lint program is complaining

    about
    > never assign a const to a non const.
    >
    > I tried this:
    > the3rdpartylib = static_cast <char *> (strSome.c_str());
    >
    > which gives compiler error message:
    > can't convert from const char * to char *



    You need to apply a const_cast, because you want to cast away
    const-ness. But you should only do this if you know that the 3rd party
    library does not modify the string you are passing. If it does, your code
    might not behave the way you would expect it to.

    hth
    --
    jb

    (replace y with x if you want to reply by e-mail)
     
    Jakob Bieling, Oct 9, 2003
    #3
  4. Steven C wrote:
    >
    > I have a 3rd party libary that takes a char * (not a const char *).
    >
    > if I have:
    >
    > string strSome;
    > char *the3rdpartylib;
    >
    > how do I:
    > the3rdpartylib = strSome.c_str();
    >
    > I have done:
    > the3rdpartylib = (char *) strSome.c_str();
    >
    > which gets by the compiler messages but the lint program is complaining about
    > never assign a const to a non const.
    >
    > I tried this:
    > the3rdpartylib = static_cast <char *> (strSome.c_str());
    >
    > which gives compiler error message:
    > can't convert from const char * to char *


    You want either of 2 things:
    1) cast away the const ness
    2) create a duplicate of the string as a C-style string

    Which strategy you choose, depends largely on what the third
    party library does with that C-style string:
    * If it leaves it alone and just does read accesses to that string
    you can take approach 1)
    * If the library modifies the passed string, you have to resort
    to approach 2)

    for 1) the3rdpartylib = const_cast< char* > ( strSome.c_str() );

    for 2) size_t len = strSome.length();
    char* tmp = new char [ len + 1 ];
    strcpy( tmp, strSome.c_str() );

    ... call the function

    strSome = tmp;
    delete [] tmp;


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 9, 2003
    #4
  5. Steven C

    Andre Kostur Guest

    (Steven C) wrote in
    news::

    > I have a 3rd party libary that takes a char * (not a const char *).
    >
    > if I have:
    >
    > string strSome;
    > char *the3rdpartylib;
    >
    > how do I:
    > the3rdpartylib = strSome.c_str();
    >
    > I have done:
    > the3rdpartylib = (char *) strSome.c_str();
    >
    > which gets by the compiler messages but the lint program is
    > complaining about never assign a const to a non const.
    >
    > I tried this:
    > the3rdpartylib = static_cast <char *> (strSome.c_str());
    >
    > which gives compiler error message:
    > can't convert from const char * to char *


    You're looking for:

    the3rdpartylib = const_cast<char *>(strSome.c_str());

    There's a couple of things to keep in mind:
    1) Ensure that the 3rd party library doesn't try to write into the string
    2) Ensure that you don't call any non-const methods on the string between
    the time you use c_str(), and whenever the pointer obtained by c_str()
    may be used.
     
    Andre Kostur, Oct 9, 2003
    #5
  6. Steven C

    klaas Guest

    Steven C wrote:

    > I have a 3rd party libary that takes a char * (not a const char *).
    >
    > if I have:
    >
    > string strSome;
    > char *the3rdpartylib;
    >
    > how do I:
    > the3rdpartylib = strSome.c_str();
    >
    > I have done:
    > the3rdpartylib = (char *) strSome.c_str();
    >
    > which gets by the compiler messages but the lint program is complaining about
    > never assign a const to a non const.
    >
    > I tried this:
    > the3rdpartylib = static_cast <char *> (strSome.c_str());
    >
    > which gives compiler error message:
    > can't convert from const char * to char *
    >
    > thanks


    what compiler do you use?
    If you are using either borland c++ or microsoft visual c++ you are
    without guarantees that you get proper behaviour on this... I'm afraid
    g++ should understand such things...

    good luck

    ebone
     
    klaas, Oct 9, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    24
    Views:
    860
    Netocrat
    Oct 30, 2005
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,105
    Ian Collins
    May 9, 2006
  3. Replies:
    11
    Views:
    1,126
  4. Javier
    Replies:
    2
    Views:
    593
    James Kanze
    Sep 4, 2007
  5. 0m
    Replies:
    26
    Views:
    1,148
    Tim Rentsch
    Nov 10, 2008
Loading...

Share This Page