C++ array assign error: invalid array assignment

C++ array assign error: invalid array assignment

Because you cant assign to arrays — theyre not modifiable l-values. Use strcpy:

#include <string>

struct myStructure
{
    char message[4096];
};

int main()
{
    std::string myStr = hello; // I need to create {h, e, l, l, o}
    myStructure mStr;
    strcpy(mStr.message, myStr.c_str());
    return 0;
}

And youre also writing off the end of your array, as Kedar already pointed out.

Why it doesnt work, if mStr.message and hello have the same data type?

Because the standard says so. Arrays cannot be assigned, only initialized.

C++ array assign error: invalid array assignment

The declaration char hello[4096]; assigns stack space for 4096 chars, indexed from 0 to 4095.
Hence, hello[4096] is invalid.

Leave a Reply

Your email address will not be published.