std::string to char*

This is a discussion on std::string to char* within the C++ Programming forums, part of the General Programming Boards category; I'm pretty noobish when it comes to the string class - I used to use char* all the time until ...

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    std::string to char*

    I'm pretty noobish when it comes to the string class - I used to use char* all the time until otherwise informed. So how would I convert a string to char*? Although best avoided, would this method *work*:

    Code:
    string str; 
    char* other_str;
     
    cin >> str;
    other_str = new char[str.size() + 1]; // would this work?
    strncpy (other_str, reinterpret_cast<char*> (str), str.size());
     
    delete [] other_str;
     
    // etc...
    I just tapped that out off the top of my head so it's probably either wrong or very noob-esque.

    I would like to know how to do this in case I need to, for example, read user input into a string instance, then pass it to an fopen call (or whatever takes a char*).
    Last edited by cboard_member; 07-15-2005 at 05:40 AM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,796
    strcpy() str.c_str()?
    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
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    c_str()? I'll look it up in MSDN. Thanks
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    don't use msdn... use http://www.cppreference.com (they only have standard things... MSDN is sometimes hard to understand, and has alot of non-standard, windows-only things)
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Lol you were right, MSDN's answer to it was about 4 lines long and almost illegible.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 02:34 PM
  2. std::string: Has my compiler gone nuts??
    By Andruu75 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2007, 04:02 AM
  3. Improving my code
    By rwmarsh in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2006, 11:18 AM
  4. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 07:08 PM
  5. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM

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