Thread: How to convert vector<char> to char*

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    How to convert vector<char> to char*

    Hi,


    How to convert vector<char> to char* ?


    thnx

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Given a vector<char> x with at least one element, you can write: &x[0]
    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
    Jan 2011
    Posts
    222
    thank you

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    With the newest C++ standard you can use x.data() to get a pointer to the data as well.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    The data member function is also available.
    std::vector::data - cppreference.com

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    You have to remember that unlike std::string::data(), std::vector::data() will not give you a sequence with null-character at the end.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    std::string::data isn't guaranteed to either. Although enough programmers rely on it to anyway, so most implementations will. But properly, you should use std::string::c_str if you want a null terminated string.
    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.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by King Mir View Post
    std::string::data isn't guaranteed to either. Although enough programmers rely on it to anyway, so most implementations will. But properly, you should use std::string::c_str if you want a null terminated string.
    I suggest you to reread the newest standard then. It IS guaranteed and both operations are equivalent.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-09-2013, 07:06 PM
  2. Replies: 9
    Last Post: 04-04-2008, 12:41 PM
  3. strcat - cannot convert char to const char
    By ulillillia in forum C Programming
    Replies: 14
    Last Post: 12-07-2006, 10:00 AM
  4. Replies: 3
    Last Post: 12-06-2006, 06:59 PM
  5. Replies: 6
    Last Post: 04-14-2002, 11:25 AM