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.

4

7 に答える 7

6

これを試すことができますstd::copy。何かのようなもの:

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

C++のやのCような文字列関数は避けます。mempcystrcpy

于 2012-07-24T07:17:38.317 に答える
3

string::copyを見てください- 文字列を取り、それを配列に入れます。

あなたの場合は次のようになります。

std::size_t length = mystr.copy(msg,10000);
msg[length]='\0';
于 2012-07-24T07:17:42.357 に答える
1

std::string のコピー メンバー関数を使用します。

size_t len = mystr.copy(msg, (sizeof msg)-1);
msg[len] = 0;
于 2012-07-24T07:20:31.860 に答える
1
char msg[10000];
string mystr = "hello";

strcpy(msg, mystr.c_str());
cout<<msg;
于 2012-07-24T07:21:06.323 に答える
0

C での文字列の割り当ては異なります。バイトを宛先文字列にコピーする必要があります。

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

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

于 2012-07-24T07:17:06.647 に答える
0

strcpy 関数を使用: 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
于 2012-07-24T07:18:47.407 に答える
0

コンパイラは、配列型に対して不安定なエラー メッセージを生成することがあります。

これは、貼り付けてコンパイルするプログラムへの以前の回答の蓄積です。

#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

関連するすべての機能のドキュメントを読むのはあなたに任せます。

于 2012-07-24T07:41:15.223 に答える