Thread: convert C++ string to C character array.

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    102

    convert C++ string to C character array.

    hello, i realy can't figure this out. i have tried to search, but i cant find any topics to match "convert string to string"
    here is what i tired:

    Code:
    #include <iostream>
    #include <string>	
    using namespace std;
    
    int main()
    {
    string stra;
    char strb[100];
    int mim;
    stra = "test";
    mim = (int)stra;
    strcpy(strb, (char)mim);
    }
    i thought this code might work.. but doesn't, i thought it would 'trick' the compiler. heh

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    std::string MyString = "Yo world!";
    char MyCharArray[1024];
    strcpy(MyCharArray, MyString.c_str());
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    YAY!
    thanks a lot.
    i really appreciate it.

    and it was so simple!
    is there some website that lists all the standard functions?

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    10
    Using : MSVS .NET 2003
    OS : Windows XP

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    thank you

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    just one more question.
    when i use 'getline(cin, string)' i input the string, then press return, and it only inputs the string after i press return twice?
    why?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >and it only inputs the string after i press return twice?
    You probably use Microsoft Visual C++ 6. That's a bug in the getline source code.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    yes!
    i have always tried to avoid using getline(), but now i can use it. Thank you
    prelude, you realy are a great member at this forum, keep up the good helping!

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    ok, sorry, i know this is a completely different question (again...) but i didn't see the point in opening another thread, when i already had a recent one.

    but anyway..

    Code:
    #include <iostream> 
    #include <string> 
    using namespace std; 
    
    int main() 
    { 
    	string x="hello world";
             cout << x[1];
    
    }
    this works fine, but if i wanted to input a character like:

    Code:
    #include <iostream> 
    #include <string> 
    using namespace std; 
    
    int main() 
    { 
    	string x;
    	cin >> x[1];
                    cout << x[1];
    
    }
    it doesn't work, why not?
    i see no reason for it not to work?

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >string x;
    This creates an empty string.

    >cin >> x[1];
    This references x[1], but does x[1] exist in an empty string?
    My best code is written with the delete key.

  11. #11
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Try this:
    Code:
    #include <iostream> 
    #include <string> 
    using namespace std; 
    
    int main() 
    { 
        string x="hello world";
        cin >> x[1];
        cout << x[1];
    }
    That works fine. The reason is because the string has a size. In your example that didn't work, you tried to change the second letter of an empty string. That is undefined behavior. You must make sure the string is big enough to access the character at the position you are trying to read into. You could also use x.resize(2) to make the size larger. I don't remember if that fills the string with null characters or spaces, but it seemed to work in my quick test.

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    102
    *bows* thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Navigating a character string array from pointers
    By Boxknife in forum C Programming
    Replies: 3
    Last Post: 04-16-2009, 01:32 PM
  2. How to put a string in a 2 dimensional character array?
    By atif7865 in forum C Programming
    Replies: 2
    Last Post: 12-05-2008, 10:26 PM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM