NEED Quick Standard Solution!

This is a discussion on NEED Quick Standard Solution! within the C++ Programming forums, part of the General Programming Boards category; Simple question, Is there a way to convert a string to char* or char [255] either will work for me. ...

  1. #1
    Banned
    Join Date
    Aug 2004
    Posts
    128

    NEED Quick Standard Solution!

    Simple question,
    Is there a way to convert a string
    to char* or char [255]
    either will work for me.
    I did a google search but to much junk to sort
    through came up and nothing that really helped!

    All replies are appreicated, and thanks in advance!

    Ex.
    Code:
    string mystring = "Hello World!";
    converted to : (but the below does not work)

    Code:
    char * mychars = mystring;
    
    or
    
    char mychars[255] = mystring;
    I even went as far as to use a for loop to copy each individual
    character from the string, but that didnt always prove successful
    i need a surefire way!

    I also tried strcpy() but it only seems to copy
    char* to char* or string to string, not string to char*
    Last edited by JarJarBinks; 11-07-2004 at 11:46 PM.

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Code:
    for(int i = 0; i < smaller_of_the_two_sizes; i++)
        mychars[i] == mystring[i];
    //don't forget to throw the null terminator at the end of mychars.
    
    OR
    
    strcpy(mychars, mystring.c_str());

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    A couple questions? Do you need to be able to modify it?
    If no:
    Code:
    mystring.c_str();
    Will return a const char *

    If you need to be able to modify it:
    Code:
    char *ptr = new char [mystring.length()+1];
    // copy from the string and append a null character
    // do whatever
    delete[] ptr;

  4. #4
    Banned
    Join Date
    Aug 2004
    Posts
    128
    No ive already done the modifying to the string,
    then the char will remain const!

    ah maybe thats why the forloop i did before didnt work
    i forgot the '\0' ok thanks, ill replay back if it worked!

  5. #5
    Banned
    Join Date
    Aug 2004
    Posts
    128
    Thank You Both Of You Guys!

    c_str(), allowed my code to compile with no errors
    or warnings, im unable to test if it works completely
    yet for the code is not complete but it should!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'Solution' and 'Project' usage in VC++
    By C+/- in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2007, 09:50 AM
  2. Getting standard input
    By winsonlee in forum C Programming
    Replies: 1
    Last Post: 04-04-2004, 09:51 PM
  3. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM
  4. C/C++ standard
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-06-2003, 04:22 AM
  5. Quick Sort Help
    By NavyBlue in forum C Programming
    Replies: 1
    Last Post: 03-02-2003, 10:34 PM

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