CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2009
    Posts
    17

    Need help with converting string to char array!

    Hey everybody.
    I am really having some big problems right now, this is totally blocking the program i am making right now, because i don't know how to do it.

    Anyways, i wrote an example code with comments so you can see exactly what i mean:

    Code:
    int main(){
    
    	string string1;
    
    	cout << "Please enter your string:" << endl;
    	cin >> string1;
    
    	char char1[x];
    	// I need some way to make x the same size as string1's length. How would i do this?
    	// For example, if i entered "hello world" as a string, that is 11 characters. It would now have to
    	// initialize: char char1[11].. So how would i do this?
    
    	// Now i need something that converts the string1 to char1[x].. How would i do this?
    	// For example, if i entered "hello world" then it would make: 
    	// char1[0] = h
    	// char1[1] = e
    	// char1[2] = l
    	// and so on, until it is done. How would i do this? I really need help :)
    
    	cout << char1;
    
    	// the cout should now be the same as you entered in cin..
    
    }
    Now, i was wondering if someone could fix my code so it works, or help me on how to do it?
    Of course many of you wouldn't want to do everything for me, but i really like to just look at a good source code and learn how things work that way.

    Anyways, some help would be awesome! And if someone will help me fix my code it would be perfect.

    ~Maxyz.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Need help with converting string to char array!

    Why do you want to convert it to a char array? The whole point of working with std::strings is so that you can avoid the messiness of C-style strings (char arrays).

    If you need to pass a const char* to a function, use the std::string's .c_str() method.

    If you need to use a function which takes a non-const char, odds are you're trying to mix C and C++ which is usually a bad idea. However, occasionally this might actually be necessary; in that case, build a std::vector<char> to hold the contents:
    Code:
    vector<char> modifiablebuffer(str.begin(),str.end());
    and then pass &modifiablebuffer[0] to the function.

    I'm not positive whether or not the above will null-terminate the array, you may need to resize the buffer one larger and put a 0 there.

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Need help with converting string to char array!

    Note: the literal : "Hello World" is 12 characters ... there is a trailing NULL
    Code:
    char * char1 = new char[string1.size()+1]; // +1 ... need to leave space for trailing NULL
    strcpy(char1,string1.c_str());
    
    
    // when done
    
    delete [] char1;
    Edit: and of course, Lindley's method is superior... no need to deallocate
    the memory at the end.
    Last edited by Philip Nicoletti; October 2nd, 2009 at 01:17 PM.

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Need help with converting string to char array!

    ... or just use the operator [] overload of std::string.

  5. #5
    Join Date
    Sep 2009
    Posts
    17

    Re: Need help with converting string to char array!

    Thanks for the great help guys.
    How would my code look with the std::string's .c_str() method
    and the
    Code:
    char * char1 = new char[string1.size()+1]; // +1 ... need to leave space for trailing NULL
    strcpy(char1,string1.c_str());
    
    
    // when done
    
    delete [] char1;
    Part put in aswell? If i understood things right?

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Need help with converting string to char array!

    Quote Originally Posted by Maxyz View Post
    Thanks for the great help guys.
    How would my code look with the std::string's .c_str() method
    and the
    Code:
    char * char1 = new char[string1.size()+1]; // +1 ... need to leave space for trailing NULL
    strcpy(char1,string1.c_str());
    // when done
    delete [] char1;
    Use std::vector as it is much safer (look up what RAII means). If you have a function that has multiple return points, this type of coding using new[]/delete[] can result in memory leaks if you forget to "delete" at one of the return points. I have seen too many examples of complex "delete[]" logic in functions that could easily be resolved by using classes such as std::vector or other classes that adheres to RAII.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; October 3rd, 2009 at 06:25 AM.

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