const char* to char* conversion

Discussion in 'C++' started by Perro Flaco, Jun 19, 2006.

  1. Perro Flaco

    Perro Flaco Guest

    Hi!

    I've got this:

    string str1;
    char * str2;
    ....
    str1 = "whatever";
    ....
    str2 = (char *)str1.c_str();

    Is this ok? Is there any other better way to do the same? I'm not sure
    if I'm doing the rigth thing, so I hope you can help me with this.

    Thanks in advanced!
     
    Perro Flaco, Jun 19, 2006
    #1
    1. Advertising

  2. Perro Flaco wrote:
    > I've got this:
    >
    > string str1;
    > char * str2;
    > ...
    > str1 = "whatever";
    > ...
    > str2 = (char *)str1.c_str();
    >
    > Is this ok? Is there any other better way to do the same? I'm not sure
    > if I'm doing the rigth thing, so I hope you can help me with this.


    No, it's not OK. And, no, there is no "better" way to do it. It's bad
    no matter how you try doing it. Why do you think you need that?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 19, 2006
    #2
    1. Advertising

  3. Perro Flaco posted:


    > str2 = (char *)str1.c_str();



    str2 = const_cast<char*>( str1.c_str() );


    I think it's valid just so long as you don't use "str2" to alter the data
    (but I'm open to correction).


    --

    Frederick Gotham
     
    Frederick Gotham, Jun 19, 2006
    #3
  4. Frederick Gotham wrote:
    > Perro Flaco posted:
    >
    >
    >> str2 = (char *)str1.c_str();

    >
    >
    > str2 = const_cast<char*>( str1.c_str() );
    >
    >
    > I think it's valid just so long as you don't use "str2" to alter the
    > data (but I'm open to correction).


    What if you use 'str1' to alter the data?
     
    Victor Bazarov, Jun 19, 2006
    #4
  5. Perro Flaco

    Azumanga Guest

    Perro Flaco wrote:
    > Hi!
    >
    > I've got this:
    >
    > string str1;
    > char * str2;
    > ...
    > str1 = "whatever";
    > ...
    > str2 = (char *)str1.c_str();
    >
    > Is this ok? Is there any other better way to do the same? I'm not sure
    > if I'm doing the rigth thing, so I hope you can help me with this.
    >

    Depends what you want to do. In your current situation, you can do the
    following:

    1) You can pass str2 to functions that expect a char*. However it is
    important that nothing changes the string at all.

    2) You must not change str1 at all, until you have finished doing
    things with str2. Any change to str1 could (and probably will) make the
    pointer str2 invalid.

    Chris
     
    Azumanga, Jun 19, 2006
    #5
  6. Perro Flaco

    abose Guest

    Not sure about your intention, but if you want to play it safe adding a
    const with str2 will help.

    string str1;
    const char * str2;
    ...
    str1 = "whatever";
    ...
    str2 = (char *)str1.c_str();

    Victor Bazarov wrote:
    > Frederick Gotham wrote:
    > > Perro Flaco posted:
    > >
    > >
    > >> str2 = (char *)str1.c_str();

    > >
    > >
    > > str2 = const_cast<char*>( str1.c_str() );
    > >
    > >
    > > I think it's valid just so long as you don't use "str2" to alter the
    > > data (but I'm open to correction).

    >
    > What if you use 'str1' to alter the data?
     
    abose, Jun 19, 2006
    #6
  7. Perro Flaco

    Perro Flaco Guest

    Because there are other functions that need "char*" as input, so I get
    errors when I do: str1.c_str()

    I would like to convert a string to a char*, or a const char* to char*.
    Any advice?

    Thanks!


    Victor Bazarov ha escrito:

    > Perro Flaco wrote:
    > > I've got this:
    > >
    > > string str1;
    > > char * str2;
    > > ...
    > > str1 = "whatever";
    > > ...
    > > str2 = (char *)str1.c_str();
    > >
    > > Is this ok? Is there any other better way to do the same? I'm not sure
    > > if I'm doing the rigth thing, so I hope you can help me with this.

    >
    > No, it's not OK. And, no, there is no "better" way to do it. It's bad
    > no matter how you try doing it. Why do you think you need that?
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask
     
    Perro Flaco, Jun 19, 2006
    #7
  8. Perro Flaco

    Perro Flaco Guest

    Ok, now I see what's going on :)

    Thank you!


    Azumanga ha escrito:

    > Perro Flaco wrote:
    > > Hi!
    > >
    > > I've got this:
    > >
    > > string str1;
    > > char * str2;
    > > ...
    > > str1 = "whatever";
    > > ...
    > > str2 = (char *)str1.c_str();
    > >
    > > Is this ok? Is there any other better way to do the same? I'm not sure
    > > if I'm doing the rigth thing, so I hope you can help me with this.
    > >

    > Depends what you want to do. In your current situation, you can do the
    > following:
    >
    > 1) You can pass str2 to functions that expect a char*. However it is
    > important that nothing changes the string at all.
    >
    > 2) You must not change str1 at all, until you have finished doing
    > things with str2. Any change to str1 could (and probably will) make the
    > pointer str2 invalid.
    >
    > Chris
     
    Perro Flaco, Jun 19, 2006
    #8
  9. Perro Flaco

    Noah Roberts Guest

    Victor Bazarov wrote:
    > Frederick Gotham wrote:
    > > Perro Flaco posted:
    > >
    > >
    > >> str2 = (char *)str1.c_str();

    > >
    > >
    > > str2 = const_cast<char*>( str1.c_str() );
    > >
    > >
    > > I think it's valid just so long as you don't use "str2" to alter the
    > > data (but I'm open to correction).

    >
    > What if you use 'str1' to alter the data?


    Nothing more nor less than what happens without the const_cast afaik.
    The pointer you got from c_str() may or may not get hosed.
     
    Noah Roberts, Jun 19, 2006
    #9
  10. Perro Flaco

    Noah Roberts Guest

    Azumanga wrote:
    > Perro Flaco wrote:
    > > Hi!
    > >
    > > I've got this:
    > >
    > > string str1;
    > > char * str2;
    > > ...
    > > str1 = "whatever";
    > > ...
    > > str2 = (char *)str1.c_str();
    > >
    > > Is this ok? Is there any other better way to do the same? I'm not sure
    > > if I'm doing the rigth thing, so I hope you can help me with this.
    > >

    > Depends what you want to do. In your current situation, you can do the
    > following:
    >
    > 1) You can pass str2 to functions that expect a char*. However it is
    > important that nothing changes the string at all.


    And the fact that they accept char* instead of const char* indicates
    that they will.
     
    Noah Roberts, Jun 19, 2006
    #10
  11. Perro Flaco

    Sgt. York Guest

    Noah Roberts wrote:
    > Azumanga wrote:
    >> Perro Flaco wrote:
    >>> Hi!
    >>>
    >>> I've got this:
    >>>
    >>> string str1;
    >>> char * str2;
    >>> ...
    >>> str1 = "whatever";
    >>> ...
    >>> str2 = (char *)str1.c_str();
    >>>
    >>> Is this ok? Is there any other better way to do the same? I'm not sure
    >>> if I'm doing the rigth thing, so I hope you can help me with this.
    >>>

    >> Depends what you want to do. In your current situation, you can do the
    >> following:
    >>
    >> 1) You can pass str2 to functions that expect a char*. However it is
    >> important that nothing changes the string at all.

    >
    > And the fact that they accept char* instead of const char* indicates
    > that they will.
    >


    Exactly. I see nothing but disaster in store for wanting to cast away
    this particular const.
     
    Sgt. York, Jun 19, 2006
    #11
  12. Perro Flaco

    Daniel T. Guest

    In article <>,
    "Perro Flaco" <> wrote:

    > Hi!
    >
    > I've got this:
    >
    > string str1;
    > char * str2;
    > ...
    > str1 = "whatever";
    > ...
    > str2 = (char *)str1.c_str();
    >
    > Is this ok? Is there any other better way to do the same? I'm not sure
    > if I'm doing the rigth thing, so I hope you can help me with this.
    >
    > Thanks in advanced!


    A better way to do it:

    string str1;
    vector<char> str2;

    str1 = "whatever";

    copy( str1.begin(), str1.end(), back_inserter( str2 ) );
     
    Daniel T., Jun 19, 2006
    #12
  13. Perro Flaco

    red floyd Guest

    Daniel T. wrote:
    > In article <>,
    > "Perro Flaco" <> wrote:
    >
    >> Hi!
    >>
    >> I've got this:
    >>
    >> string str1;
    >> char * str2;
    >> ...
    >> str1 = "whatever";
    >> ...
    >> str2 = (char *)str1.c_str();
    >>
    >> Is this ok? Is there any other better way to do the same? I'm not sure
    >> if I'm doing the rigth thing, so I hope you can help me with this.
    >>
    >> Thanks in advanced!

    >
    > A better way to do it:
    >
    > string str1;
    > vector<char> str2;
    >
    > str1 = "whatever";
    >
    > copy( str1.begin(), str1.end(), back_inserter( str2 ) );


    You forgot:

    str2.push_back('\0');
    APICall(&str2[0]);
     
    red floyd, Jun 19, 2006
    #13
  14. Perro Flaco wrote:

    > Because there are other functions that need "char*" as input, so I get
    > errors when I do: str1.c_str()
    >
    > I would like to convert a string to a char*, or a const char* to char*.
    > Any advice?



    When some function need a 'char*' (as opposed to 'const char*') as an input,
    this usually means one of two things:

    1) The function needs to modify the data pointed by that 'char*' pointer. In
    this can what you are trying to do is simply useless, because strings of
    'std::string' type cannot be modified through the pointer returned by 'c_str()'.

    Functions that modify the data pointed by 'char*' parameter are not compatible
    with 'std::string' at all. The only safe way around is to convert the
    'std::string' to a standalone modifiable zero-terminated C-style string, pass it
    to the function and then convert the results back to 'std::string'.

    2) The function parameter is declared improperly. In other words, the function
    doesn't really need to modify the data, i.e. it doesn't really need a 'char*'
    and a 'const char*' would work just as well. The better way to resolve the
    problem in this case would be to change the function's parameter declaration
    from 'char*' to the more appropriate 'const char*', assuming this is possible.
    Otherwise, the 'const_cast' of 'c_str()' result to 'char*' type (mentioned by
    others) would work, but it is rather ugly.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Jun 19, 2006
    #14
  15. Daniel T. wrote:
    > In article <>,
    > "Perro Flaco" <> wrote:
    >
    >> Hi!
    >>
    >> I've got this:
    >>
    >> string str1;
    >> char * str2;
    >> ...
    >> str1 = "whatever";
    >> ...
    >> str2 = (char *)str1.c_str();
    >>
    >> Is this ok? Is there any other better way to do the same? I'm not
    >> sure if I'm doing the rigth thing, so I hope you can help me with
    >> this.
    >>
    >> Thanks in advanced!

    >
    > A better way to do it:
    >
    > string str1;
    > vector<char> str2;
    >
    > str1 = "whatever";
    >
    > copy( str1.begin(), str1.end(), back_inserter( str2 ) );


    If str2 is not used up until here, the form

    string str1;
    ..
    str1 = "whatever";
    ..
    vector<char> str2(str1.begin(), str1.end());

    is actually better, IMO. It can't be helped, of course, if 'str2' is
    a member of the same class as 'str1'.

    We have, however, already seen the reason why the OP needed the pointer
    and vector is not a good substitute.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 19, 2006
    #15
  16. Perro Flaco

    Perro Flaco Guest

    Yes, this is what I've done. We don't need the "char*" and a "const
    char*" would do a perfect work.

    Thanks!


    Andrey Tarasevich ha escrito:

    > Perro Flaco wrote:
    >
    > > Because there are other functions that need "char*" as input, so I get
    > > errors when I do: str1.c_str()
    > >
    > > I would like to convert a string to a char*, or a const char* to char*.
    > > Any advice?

    >
    >
    > When some function need a 'char*' (as opposed to 'const char*') as an input,
    > this usually means one of two things:
    >
    > 1) The function needs to modify the data pointed by that 'char*' pointer. In
    > this can what you are trying to do is simply useless, because strings of
    > 'std::string' type cannot be modified through the pointer returned by 'c_str()'.
    >
    > Functions that modify the data pointed by 'char*' parameter are not compatible
    > with 'std::string' at all. The only safe way around is to convert the
    > 'std::string' to a standalone modifiable zero-terminated C-style string, pass it
    > to the function and then convert the results back to 'std::string'.
    >
    > 2) The function parameter is declared improperly. In other words, the function
    > doesn't really need to modify the data, i.e. it doesn't really need a 'char*'
    > and a 'const char*' would work just as well. The better way to resolve the
    > problem in this case would be to change the function's parameter declaration
    > from 'char*' to the more appropriate 'const char*', assuming this is possible.
    > Otherwise, the 'const_cast' of 'c_str()' result to 'char*' type (mentioned by
    > others) would work, but it is rather ugly.
    >
    > --
    > Best regards,
    > Andrey Tarasevich
     
    Perro Flaco, Jun 19, 2006
    #16
  17. Perro Flaco

    Noah Roberts Guest

    Perro Flaco wrote:
    > Yes, this is what I've done.


    "this"??
     
    Noah Roberts, Jun 19, 2006
    #17
  18. Perro Flaco

    Jim Langston Guest

    "Sgt. York" <> wrote in message
    news:...
    > Noah Roberts wrote:
    >> Azumanga wrote:
    >>> Perro Flaco wrote:
    >>>> Hi!
    >>>>
    >>>> I've got this:
    >>>>
    >>>> string str1;
    >>>> char * str2;
    >>>> ...
    >>>> str1 = "whatever";
    >>>> ...
    >>>> str2 = (char *)str1.c_str();
    >>>>
    >>>> Is this ok? Is there any other better way to do the same? I'm not sure
    >>>> if I'm doing the rigth thing, so I hope you can help me with this.
    >>>>
    >>> Depends what you want to do. In your current situation, you can do the
    >>> following:
    >>>
    >>> 1) You can pass str2 to functions that expect a char*. However it is
    >>> important that nothing changes the string at all.

    >>
    >> And the fact that they accept char* instead of const char* indicates
    >> that they will.
    >>

    >
    > Exactly. I see nothing but disaster in store for wanting to cast away
    > this particular const.


    I agree. Unfortunately I use a library with a header which is not const
    correct. I've been trying to get the library developer to fix his const
    correctness, but so far to little luck.

    I wind up using things like:

    somefunction( blah, blah, const_cast<char*>( MyString.c_str() ), blah );
    and it's quite ugly and I don't like it, but have little choice until he
    fixes his library (or I hack the header).

    Which brings up a question that is probably OT for this newsgroup. If a
    program is linking to a .lib and using a header, can the header be changed
    to const correctness even if the calls in the .dll aren't const correctness?
    That is, if it is known for a fact that the .dll won't change the data of a
    char*, can changing the header to a const char * cause problems? If this is
    OT (which it is) just ignore it please.
     
    Jim Langston, Jun 20, 2006
    #18
  19. Jim Langston wrote:
    > [..]
    > If a program is linking to a .lib and using a header, can the header
    > be changed to const correctness even if the calls in the .dll aren't
    > const correctness?


    Only if the linkage of those functions is "C". Otherwise, the type of
    the arguments is part of the type of the function, and likely its name.

    > That is, if it is known for a fact that the .dll
    > won't change the data of a char*, can changing the header to a const
    > char * cause problems? If this is OT (which it is) just ignore it
    > please.


    Yes, it can cause problems. And, yes, it is slightly OT because it's
    implementation- and platform-specific.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 20, 2006
    #19
  20. Perro Flaco

    Sgt. York Guest

    Jim Langston wrote:
    > "Sgt. York" <> wrote in message
    > news:...
    >> Noah Roberts wrote:
    >>> Azumanga wrote:
    >>>> Perro Flaco wrote:
    >>>>> Hi!
    >>>>>
    >>>>> I've got this:
    >>>>>
    >>>>> string str1;
    >>>>> char * str2;
    >>>>> ...
    >>>>> str1 = "whatever";
    >>>>> ...
    >>>>> str2 = (char *)str1.c_str();
    >>>>>
    >>>>> Is this ok? Is there any other better way to do the same? I'm not sure
    >>>>> if I'm doing the rigth thing, so I hope you can help me with this.
    >>>>>
    >>>> Depends what you want to do. In your current situation, you can do the
    >>>> following:
    >>>>
    >>>> 1) You can pass str2 to functions that expect a char*. However it is
    >>>> important that nothing changes the string at all.
    >>> And the fact that they accept char* instead of const char* indicates
    >>> that they will.
    >>>

    >> Exactly. I see nothing but disaster in store for wanting to cast away
    >> this particular const.

    >
    > I agree. Unfortunately I use a library with a header which is not const
    > correct. I've been trying to get the library developer to fix his const
    > correctness, but so far to little luck.
    >
    > I wind up using things like:
    >
    > somefunction( blah, blah, const_cast<char*>( MyString.c_str() ), blah );
    > and it's quite ugly and I don't like it, but have little choice until he
    > fixes his library (or I hack the header).
    >


    I would go out of my way to avoid this. I would use a temporary despite
    the performance hit.

    -York
     
    Sgt. York, Jun 20, 2006
    #20
    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. Thomas Matthews
    Replies:
    5
    Views:
    2,554
    tom_usenet
    Aug 2, 2004
  2. Santa
    Replies:
    1
    Views:
    1,160
    Mark A. Odell
    Jul 17, 2003
  3. Replies:
    24
    Views:
    906
    Netocrat
    Oct 30, 2005
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,153
    Ian Collins
    May 9, 2006
  5. Javier
    Replies:
    2
    Views:
    625
    James Kanze
    Sep 4, 2007
Loading...

Share This Page