View Full Version : problem with a string C/C++ =[

09-17-2007, 09:24 PM
I think this is going to sound really stupid but:

char buffer[256];
buffer = "example of a line\n";

I'm getting a: "incompatible types in assignment":
a declaration like: char * buffer is giving me the same problem.

i must be missing something really obvious here.

ralph l mayo
09-17-2007, 10:35 PM
"example of a line\n" is a const char*, so you can't assign it to a char* without either (a) doing it at the same time you declare the (const char*) buffer or (b) adding an intermediate step, most commonly strcpy(char* dest, const char* source). Casting also works for (b) but strcpy is more correct.

In C++ you almost certainly want to use std::string instead and duck the whole issue.

09-17-2007, 11:42 PM
Hey Ralph, yeah a const, what was i thinking!...