Thread: string to char array

  1. #1
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128

    string to char array

    Ok, i've searched around and got nothing.

    Is it possible to convert a data type string into a char array?
    Here is an example:
    Code:
      
    string name = "Myname";
    char *name_array = name;
    I am figuring that there may be a function to allow you to do this, or a way of getting around it. Any ideas?
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Code:
    string name = "endo";
    char* ptr = name.c_str( );
    Couldn't think of anything interesting, cool or funny - sorry.

  3. #3
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    I get the following error message:

    initialization to `char *' from `const char *' discards qualifiers
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    It returns a pointer to const data, otherwise, you could access the private data in the string and alter it.....not good!!!

    Code:
    #include<iostream>
    #include<cstring>
    #include<string>
    
    
    
    int main(){
    
    	std::string name = "endo!!";
    
    	std::cout << name << std::endl;//ok so far
    
    	/*now for something awful!*/
    
    	char* ptr = const_cast<char*>(name.c_str());//You could..
    
    	/*but then!!!! Agh!!!!!*/
    
    	strcpy(ptr,"Fordy!");//surely not!!!!!
    
    	std::cout << name << std::endl;//nasty!!!!	
    
    	return 0;
    
    }
    Best bet...assign it to a const char*...you shouldnt expect to change that char array....so it can safely be const...

    If you want to alter a copy of the string...then use the normal string copying methods

    Code:
    char copy[10];
    strcpy(copy,name.c_str());
    or something....

  5. #5
    Registered User fry's Avatar
    Join Date
    Mar 2002
    Posts
    128
    I see. Thanks for the info.
    It all works now
    IDE: Dev C++ 5
    Lib: Allegro
    OS: Windows 2000

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    std::string has two member functions which convert the string to an array of characters. The first, c_str(), transforms the string into an array of characters with a terminating nul character ('\0') at the end to designate a C style string. The second and less useful is data(), which converts that string directly to an array of characters, no nul terminator is added:

    std::string s = "HappyFrosty"

    s.c_str()
    {'H','a','p','p','y','F','r','o','s','t','y','\0'}
    s.data()
    {'H','a','p','p','y','F','r','o','s','t','y'}

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Character arrays
    By PsychoBrat in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2002, 12:02 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM