c++stringstdcstring

C++ string to fixed sized char array possible?


Hi i have the following code:

char msg[10000];
string mystr = "hello";

I want to put mystr into msg. Is there a way to do that? I tried all sorts of methods, but keep getting:

incompatible types in assignment of 'const char*' to char [10000]'

I tried:

msg = mystr.c_str();

and

msg = (char[10000])mystr;

to no avail.


Solution

  • You can try std::copy for this. Something like:

    std::copy(mystr.begin(), mystr.end(), msg);
    

    I would avoid C string functions like mempcy and strcpy in C++.