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

This is a discussion on how to covert 'std::string' to 'char * in c++ within the C++ Programming forums, part of the General Programming Boards category; Hi all 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
    21,993
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,993
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    std::string contents("something");
    std::vector<char> buff(contents.begin(), contents.end());
    &buff[0] could be used as modifiable char array
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    21,993
    DOes it mean i need to use "strcpy" function?
    Yes, or you could use what vart suggested.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,340
    >> 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,053
    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
    21,993
    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);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21