CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2006
    Posts
    3

    How to convert a string into an array of char?

    Hi.

    I want to convert a string into an array of chars but I have no idea how to do it. Someone please help.

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: How to convert a string into an array of char?

    The function is 'std::string::c_str()':
    Code:
    std::string myString = "This is my string!";
    const char *pArrayOfChars = myString.c_str();
    assert(pArrayOfChars[2] == 'i');
    Viggy

  3. #3
    Join Date
    Nov 2006
    Location
    Barcelona - Catalonia
    Posts
    364

    Re: How to convert a string into an array of char?

    It could be something like:

    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    #define SIZE 30
    
    int main(void)
    {
    	string sStr="Hi, World";
    	char sArr[SIZE];
    
    	memset(sArr, 0, SIZE); //Inicialize array
    	memcpy(sArr, sStr.c_str(), strlen(sStr.c_str())); //copy Array
    	
    	cout << sArr << endl;
    }
    Albert.

  4. #4
    Join Date
    Dec 2006
    Posts
    3

    Re: How to convert a string into an array of char?

    Thanks viggy, but i do not fully understand it. Could you add comments on each line please?

  5. #5
    Join Date
    Dec 2006
    Posts
    3

    Re: How to convert a string into an array of char?

    No, there is no need for that. I solved it thanks to albert's advice ^^ thanks albert!

  6. #6
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582

    Re: How to convert a string into an array of char?

    Or, if you don't need your own copy, I'd stick with Viggy's advice:
    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	string sStr ="Hi, World";
    	const  char* sArr = sStr.c_str();
    	cout << sArr << endl;
    }
    Jeff

  7. #7
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: How to convert a string into an array of char?

    Quote Originally Posted by AlbertGM
    Code:
    int main(void)
    Writing void like that is redundant in C++ and doesn't make it look good.
    Quote Originally Posted by AlbertGM
    Code:
    memset(sArr, 0, SIZE); //Inicialize array
    No need to initialize this way - setting the first character to '\0' is enough for C-style strings.
    Quote Originally Posted by AlbertGM
    Code:
    memcpy(sArr, sStr.c_str(), strlen(sStr.c_str())); //copy Array
    Prone to going beyond sArr buffer size. Either provide a check for sStr length and SIZE or use dynamic allocs.

    //Are you fat Albert from Hitch? kiddin'..

  8. #8
    Join Date
    Mar 2006
    Posts
    55

    Re: How to convert a string into an array of char?

    I tried using memcpy and by using viggy's method but each time i get an error saying that c_str() is not declared

  9. #9
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582

    Re: How to convert a string into an array of char?

    Quote Originally Posted by Tsubasa
    I tried using memcpy and by using viggy's method but each time i get an error saying that c_str() is not declared
    Post your code, please.

    Jeff

  10. #10
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: How to convert a string into an array of char?

    convert a string to char array???

    a std::string IS a char array!
    well not explicitly by stl....

    and what is a "string"? is it char x[] = "ASDF" or char* x = "ASDF"; or std::string x = "SDFG" or CString x = "ASDF"; or wchar_t or TCHAR isntead of char.. or something else??

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured