Thread: how to covert 'std::string' to 'char * in c++

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    78

    Smile how to covert 'std::string' to 'char * in c++

    Hi all


    how to covert 'std::string' to 'char * in c++

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    You can "convert" a std::string to a const char* using the c_str() member function.

    If you need a modifiable char* instead, then you have to copy over the characters.
    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
    Dec 2007
    Posts
    78
    i want it to be modifiable, can you help?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    i want it to be modifiable, can you help?
    Yes, but why not just use std::string?

    As I said, you just need to copy over the characters.
    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

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    std::string contents("something");
    std::vector<char> buff(contents.begin(), contents.end());
    &buff[0] could be used as modifiable char array
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    78
    I have a standard function which nedds "char* " to be modifiable...

    I didnt get what you mean by "copy over the characters."

    DOes it mean i need to use "strcpy" function?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    DOes it mean i need to use "strcpy" function?
    Yes, or you could use what vart suggested.
    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
    Dec 2007
    Posts
    78
    ya i got it!!!!

    Thanks alot ,laserlight & vast !!!!!

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If the modified string will be larger than the original string and you're using a vector<char>, be sure to call vector's reserve() member function first to reserve enough space to store the new string...

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I have a standard function which nedds "char* " to be modifiable
    If it is a "standard" function, there's probably a better alternative that works on strings.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by vart View Post
    Code:
    std::string contents("something");
    std::vector<char> buff(contents.begin(), contents.end());
    &buff[0] could be used as modifiable char array
    Quote Originally Posted by chintugavali View Post
    I have a standard function which nedds "char* " to be modifiable...

    I didnt get what you mean by "copy over the characters."

    DOes it mean i need to use "strcpy" function?
    Quote Originally Posted by laserlight View Post
    Yes, or you could use what vart suggested.
    Not if you want to follow the standard. &buff[0] is a modifiable char array, but it is not guaranteed to be null terminated. On the other hand strcpy requires the string to be null terminated. So you should use &buff[0] if the string length is specified, and strcpy if the string must be null terminated.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Not if you want to follow the standard. &buff[0] is a modifiable char array, but it is not guaranteed to be null terminated.
    Good point. In fact, it will not be null terminated since vart's example copied the actual string elements based on an iterator pair.

    On the other hand strcpy requires the string to be null terminated. So you should use &buff[0] if the string length is specified, and strcpy if the string must be null terminated.
    Or we could modify vart's example to:
    Code:
    std::string contents("something");
    std::vector<char> buff(contents.c_str(), contents.length() + 1);
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  2. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM