1

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?

1

5 Answers 5

7
strcpy (&name, s.c_str());
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

0

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

1 Comment

One caveat on strncpy(): it won't null-terminate if the source string is longer than the length argument.
0

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>.

Comments

0
std::memcpy(name, str.c_str(), str.size() + 1);

Comments

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.