【发布时间】:2011-05-06 08:19:01
【问题描述】:
我不是 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。
-
hello[4096] = 0;行是错误的。这是数组的最后一个元素。只需删除此行。