CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Sep 2003
    Posts
    815

    casting from std:string to char *

    How do I cast from std:string to char *?

    Thanks
    avi123

  2. #2
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    You can't do that. The only thing you can get from an std::string is a const char *, which means that you can't modify it. You get this by calling string::c_str()
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  3. #3
    Join Date
    Sep 2003
    Posts
    815
    can I convert const char* to char* ?

  4. #4
    Join Date
    Sep 2003
    Posts
    815
    I didn't mean the last question seriously..

  5. #5
    Join Date
    Aug 2001
    Location
    Texas
    Posts
    645
    The "const char *" means that you can't modify it.

    Are you wanting to modify the string value by manipulating
    the buffer directly??

    What is the reason that you are wanting the "char *" instead
    of the "const char *"?

  6. #6
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Originally posted by avi123
    can I convert const char* to char* ?
    You can, but it's very dangerous. The way to do it is to use const_cast as described in this FAQ item. You should not modify the buffer directly, but there may be valid reasons to do the cast. One that springs to mind is when you want to call a C-style function (from Windows, another dll etc.) that will basically never change the string, but doesn't have the const in its declaration.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  7. #7
    Join Date
    Sep 2003
    Posts
    815
    I need to be able to work on this string but it doesn't have to be the returned string, so I guess I can put it on a new buffer of char * type is that right?

    I mean can I do:

    const char* str1;
    char* str2;
    str2 = (char*)str1

    is that ok?

  8. #8
    Join Date
    Aug 2001
    Location
    Texas
    Posts
    645
    Don't change the size of the string.

  9. #9
    Join Date
    Sep 2003
    Posts
    815
    another question:

    I used string::c_str() as suggested at the compilation passed thought I tried to changed the return value, is that ok?

    I mean I did:
    const char * string= stdString.c_str();
    and the compilation passed when tring to change string

  10. #10
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Originally posted by avi123
    I need to be able to work on this string but it doesn't have to be the returned string, so I guess I can put it on a new buffer of char * type is that right?

    I mean can I do:

    const char* str1;
    char* str2;
    str2 = (char*)str1

    is that ok?
    No that is not OK. The (char *) is a c-style cast that removes the constness of str1. C-style casts should not be used anymore (OK, I do sometimes use them) but here it's basically a const_cast. And as I have pointed out above, it's a dangerous thing to do.

    If you want to modify it, you'll have to make a non-const copy of the string.
    Code:
    string s = "Hello";
    char *sz;
    
    sz = new char[s.length() + 1];
    strcpy(sz, s.c_str());
    // now work with sz, which is a copy of s
    
    // clean up
    delete [] sz;
    const is there for a reason. The reason is that you should not modify a const object. If you try to modify it, it could lead to all sorts of strange problems and undefined behaviour.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  11. #11
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Originally posted by avi123
    another question:

    I used string::c_str() as suggested at the compilation passed thought I tried to changed the return value, is that ok?

    I mean I did:
    const char * string= stdString.c_str();
    and the compilation passed when tring to change string
    That's a bug in your compiler. It should not allow changing a const object.

    Another thing is that you should not call your variable string, since string is already std::string.

    Compiling the following on VC.NET gives me an error (as it should):
    Code:
    string s = "Hello";
    const char *sz = s.c_str();
    sz[0] = 'B';
    And the error is pretty descriptive:
    Error: l-value specifies const object
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  12. #12
    Join Date
    Sep 2003
    Posts
    815
    well I don't know what was the problem but bow I got the error which is good.

    How do I cast in the different direction?
    I mean I have a char* (for example "1234567890")
    and I want to put in a std:string?

    Thanks again

  13. #13
    Join Date
    Aug 2001
    Location
    Texas
    Posts
    645
    Is this what you mean?

    Code:
    char *s1 = "123456";
    string s2 = s1;

  14. #14
    Join Date
    Sep 2003
    Posts
    815
    yep

    is that also ok?

    char *s1 = "123456";
    string s2(s1);

  15. #15
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Yes, that's OK.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured