tags:

views:

225

answers:

5

If I have something like

char name[10];

and I want to put the string s into name, where s = "joe"

how would I do that?

Also, can I make a function that takes strings as inputs, but treats those as char arrays?

A: 

strcpy() will generally get the job done. strncpy() is better, if available.

Pestilence
One caveat on strncpy(): it won't null-terminate if the source string is longer than the length argument.
Fred Larson
+1  A: 

std::string has a c_str member that converts it to const char*. To copy from one char array to another use strcpy.

Nikola Smiljanić
+2  A: 

strcpy (&name, s.c_str());

Ilias Bartolini
A: 

If you have a C++ string, you can call its c_str() method to get a char *, suitable for using with strcpy(), defined in <cstring>.

Javier Badia
A: 
std::memcpy(name, str.c_str(), str.size() + 1);
Tronic