2

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.

jogojapan
  • 68,383
  • 11
  • 101
  • 131
John Marston
  • 11,549
  • 7
  • 23
  • 18
  • 1
    possible duplicate of [How to convert string to char * in C++?](http://stackoverflow.com/questions/9309961/how-to-convert-string-to-char-in-c) – RedX Jul 24 '12 at 07:20
  • 1
    possible duplicate of [how to copy char * into a string and vice-versa](http://stackoverflow.com/questions/2564052/how-to-copy-char-into-a-string-and-vice-versa) – jogojapan Jul 24 '12 at 07:21

7 Answers7

6

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

cnicutar
  • 178,505
  • 25
  • 365
  • 392
3

Take a look at string::copy - it takes a string an puts it into an array.

In your case it would be:

std::size_t length = mystr.copy(msg,10000);
msg[length]='\0';
colin
  • 284
  • 2
  • 12
Daniel
  • 1,079
  • 1
  • 11
  • 25
1

Use copy member function of std::string:

size_t len = mystr.copy(msg, (sizeof msg)-1);
msg[len] = 0;
perreal
  • 94,503
  • 21
  • 155
  • 181
1
char msg[10000];
string mystr = "hello";

strcpy(msg, mystr.c_str());
cout<<msg;
cppcoder
  • 22,227
  • 6
  • 56
  • 81
0

String assignment in C is different. You have to copy the bytes into your destination string.

memcpy_s(msg, 1000, mystr.c_str(), mystr.length()) // safe windows version

memcpy(msg, mystr.c_str(), mystr.length()) // unix version

RedX
  • 14,749
  • 1
  • 53
  • 76
  • This will result in a buffer overflow if the message is longer than 10000 characters, right? It should probably be mystr.length() > 10000 ? 10000 : mystr.length() for the last argument or something like that. – Sarien Jul 24 '12 at 07:19
0

Use strcpy function : http://www.cplusplus.com/reference/clibrary/cstring/strncpy/

strncpy(msg, mystr.c_str(), sizeof msg / sizeof msg[0]);
msg[sizeof msg / sizeof msg[0] - 1] = 0; // null-terminate in case of truncation
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
A. K.
  • 34,395
  • 15
  • 52
  • 89
0

Compilers sometimes produce wonky error messages for array types.

Here's an accumulation of previous answers into a paste-and-compile program.

#include <string>
#include <iostream>

#if 1

int main(int argc, char **argv)
{
    using std::cout;
    using std::endl;

    char msg[1000] = {0};    // initialize to 0 here since we're printing below
                            // the <type> <array-name>[<size>] = {0} just fills a POD struct or an array with 0s

    std::string mystr = "hello";

    // if, at some point, you have things changing "mystr"
    // you'll need to make sure that it will fit in msg[]

    cout << "Before strcpy: \"" << msg << "\"" << endl;

    // I'll just finish the statement in mystr...
    mystr += " world!";

    if(mystr.length() < sizeof(msg)){
        strcpy(
            msg,            // <- put in here until we find a '\0'
            mystr.c_str()    // <- take from here (which could be a temporary buffer)
            );
    }

    //MSC will complain about strcpy being unsafe
    //
    // you can use the below instead (if you really feel the need to), which is
    // the MS-specific equivalent to the above.
    /*
        strcpy_s(
            msg,            // <- put in here until we find a '\0' or the size limit is reached
            sizeof(msg),    // <- don't put any more than this many chars in msg
            mystr.c_str()    // <- take from here
            );
    */

    cout << "After strcpy: \"" << msg << "\"" << endl;

    return 0;
}

#else

// Similarly, using wchar_t (a usually non-byte-sized character type)
//
// note where the divisions occurr

int main(int argc, char **argv)
{
    using std::wcout;
    using std::endl;

    wchar_t msg[1000] = {0};
    std::wstring mystr = L"hello";

    wcout << "Before strcpy: \"" << msg << "\"" << endl;

    mystr += L" world";

    if(mystr.length() < (sizeof(msg)/sizeof(wchar_t))){
        // mystr wil fit!
        wcscpy(
            msg,            // <- put in here until we find a '\0'
            mystr.c_str()    // <- take from here (which could be a temporary buffer)
            );
    }

    // Similar to the char case in the first preprocessor block
    /*
        wcscpy_s(
            msg,                            // <- put in here until we find a '\0' or the size limit is reached
            sizeof(msg)/sizeof(wchar_t),    // <- don't put any more than this many wchar_ts in msg
            mystr.c_str()                    // <- take from here
            );
    */

    wcout << "After strcpy: \"" << msg << "\"" << endl;

    return 0;
}

#endif

I shall leave it to you to read the documentation on all related functions.

defube
  • 2,395
  • 1
  • 22
  • 34