我不是C++程序员,所以我需要一些数组帮助。 我需要为一些结构分配一个字符数组,例如C++数组赋值错误:无效数组赋值
struct myStructure {
char message[4096];
};
string myStr = "hello"; // I need to create {'h', 'e', 'l', 'l', 'o'}
char hello[4096];
hello[4096] = 0;
memcpy(hello, myStr.c_str(), myStr.size());
myStructure mStr;
mStr.message = hello;
我得到error: invalid array assignment
为什么它不工作,如果mStr.message
和hello
具有相同的数据类型?
您必须使用strcpy或memcpy函数而不是mstr.message = hello。 – Siddiqui 2010-11-07 17:18:02
行'hello [4096] = 0;'是错误的。这是数组中最后一个元素的一个。只要删除这一行。 – 2010-11-07 17:27:57