Adding/Converting String to Char[]

This is a discussion on Adding/Converting String to Char[] within the C++ Programming forums, part of the General Programming Boards category; Is it possible? I've tried a few things, but no cigar. Thanks guys/gals!...

  1. #1
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114

    Adding/Converting String to Char[]

    Is it possible?
    I've tried a few things, but no cigar.
    Thanks guys/gals!
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Maybe with c_str() ?
    I hate real numbers.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    Note that c_str() only returns const char*, not char*, so you cannot modify the string.
    There's no easy way to create a char array from a std::string. You can blame the C++ standards committee for that.
    But I believe the following works:

    std::string mystr = "My string";
    std::vector<char> v(mystr.begin(), mystr.end());

    Though I may be wrong.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Jeez, that was unexpected.
    Thanks a heap guys, that was extremely helpful.
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    There's usually no need to convert a string to a char[]. Are you sure you really need to do that?

  6. #6
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Yep.
    100&#37; sure.
    OS: Windows XP Home Edition SP3, Windows 7 Ultimate Beta Build 7000
    LANGUAGES: C++, VB6
    SKILL: Novice/Intermediate

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by Elysia View Post
    There's no easy way to create a char array from a std::string.
    Well, depending on what you mean by easy. If const is good enough, it's ok, but if not, then you just need to dynamically allocate a new buffer and copy over the string. Outside of deleting the memory, it's not all that horrible.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    Quote Originally Posted by MacGyver View Post
    Well, depending on what you mean by easy. If const is good enough, it's ok, but if not, then you just need to dynamically allocate a new buffer and copy over the string. Outside of deleting the memory, it's not all that horrible.
    Ya, I mean besides for const char*.
    It's really a horrible workaround for functionality that should be there in the first place IMHO. But I digress. Can't change the facts, after all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> It's really a horrible workaround for functionality that should be there in the first place IMHO.
    I guess you're talking about making a GetBuffer style function available for string? I actually wouldn't be surprised if that was available in a future technical report, but I don't think it's that big of a deal to do without.

    >> Yep. 100&#37; sure.
    I honestly don't believe you 100% but it's not a big deal and you can do whatever you want with your program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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