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?
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?
strcpy()
will generally get the job done. strncpy()
is better, if available.
std::string
has a c_str
member that converts it to const char*
. To copy from one char array to another use strcpy
.
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>
.