C ++:expression必须是可修改的左值

时间:2016-01-31 17:26:51

标签: c++ c-strings

char message[512];
string info;
...
// read from txt file and save to "info"
...
message = info.c_str();

错误是由最后一行的“消息”引起的。我不熟悉C或C ++,有人能告诉我出了什么问题吗?

2 个答案:

答案 0 :(得分:2)

const char *message; // needs to be a pointer, not an actual array
string info;
...
// read from txt file and save to "info"
...
message = info.c_str();

答案 1 :(得分:1)

你可以这样做:

strncpy(message, info.c_str(), sizeof(message));
message[sizeof(message) - 1] = '\0';