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

The error is caused by "message" on the last line. I'm not familiar with C or C++, can someone tell me what went wrong?

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
nomnom
  • 1,560
  • 5
  • 17
  • 31
  • char message is an array of 512 chars. The return value of info.c_str() is a pointer to an array of chars. See this http://stackoverflow.com/questions/18491883/deep-copy-stdstringc-str-to-char (there is no automatic copying from the data pointed to by c_str() into the array) – Daniel F Jan 31 '16 at 17:29
  • 2
    Please use either the C or C++ tag, not both; they are separate languages. I have edited the tags for you. – Colonel Thirty Two Jan 31 '16 at 17:38

2 Answers2

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();
Gavriel
  • 18,880
  • 12
  • 68
  • 105
  • Thanks, I got a new error though, this time it's on the "=" mark on the last line. It says: a value of type const char * cannot be assigned to an entity of type char * – nomnom Jan 31 '16 at 17:34
  • Variables prefixed with const must be initialized during creation. const char * c = str.c_str(); – Daniel F Jan 31 '16 at 17:35
  • 1
    @DanielF That's not true in the case of `const char*`; in that case, the data pointed to by the pointer is const, not the pointer itself. Your previous revision was missing the `const`. – Colonel Thirty Two Jan 31 '16 at 17:40
  • @ColonelThirtyTwo, just as I thought ;) – Gavriel Jan 31 '16 at 17:42
1

You can do this:

strncpy(message, info.c_str(), sizeof(message));
message[sizeof(message) - 1] = '\0';
Viktor Simkó
  • 2,607
  • 16
  • 22