Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
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?
std::string
strcpy (&name, s.c_str());
Add a comment
std::string has a c_str member that converts it to const char*. To copy from one char array to another use strcpy.
c_str
const char*
strcpy
strcpy() will generally get the job done. strncpy() is better, if available.
strcpy()
strncpy()
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>.
c_str()
char *
<cstring>
std::memcpy(name, str.c_str(), str.size() + 1);
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
std::stringinterface. This is a good place to start: cplusplus.com/reference/string/string Or this: sgi.com/tech/stl/basic_string.html