Thread: Converting strings to chars

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    Converting strings to chars

    I want to convert few strings, ex:
    Code:
    string1 = "abc";
    string2 = "123";
    to array of characters, ex:
    char1[0] = 'a';
    char1[1] = 'b';
    char1[2] = 'c';

    char2[0] = '1';
    char2[1] = '2';
    char2[2] = '3';
    How can I do this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    char foo[10];
    strcpy( foo, string.c_str() );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Code:
    BYTE Foo[10];
    DWORD Index = 0;
    
    
    while((string1[Index] !=0) && (Index < 10)){
         CopyMemory(&Foo[Index] , &string1[Index] , 1);
         Index++;
         }
    This code avoids overruns in the case where the string isnt properly termianted with a NULL

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, actually, it doesn't. If the string is 3 characters long, but doesn't have a NULL terminator, you just keep copying. And your code acts like strncpy(): if the string is longer than 10 characters, you don't add a terminator to it.

    And to top it off, that code is Windows-only. Why do it unportably if you can do it portably? At the very least, mention that it is Windows-specific, especially when the OP said nothing about Windows programming.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And why use a range copy call when the = has the same semantics? For PODs, this:

    CopyMemory(&Foo[Index] , &string1[Index] , 1);

    is the same as

    Foo[Index] = string1[Index];

    except slower, more complicated, less readable and more error-prone.

    To get a mutable C-style array of characters, I'd use this:
    Code:
    std::vector<char> v(string1.begin(), string1.end());
    FunctionThatWantsCharPtr(&v[0], v.size());
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting strings to char
    By Furious5k in forum C++ Programming
    Replies: 13
    Last Post: 01-02-2009, 05:26 PM
  2. strings and char's
    By mikecompsc in forum C++ Programming
    Replies: 3
    Last Post: 05-21-2007, 01:26 AM
  3. converting a vector of strings into an array.
    By LightsOut06 in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 07:14 PM
  4. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM
  5. converting chars to ascii equivilant
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-01-2001, 12:03 PM