struct testing
{
char lastname[20];
};
testing *pt = new testing;
pt->lastname = "McLove";
and I got
56 C:\Users\Daniel\Documents\Untitled2.cpp incompatible types in
assignment of ‘const char[7]’ to ‘char[20]’
Why ?
Thanks in advance.
Because compile time arrays are constant. In your struct
testing, you have an array of 20chars, and you’re trying to assign a pointer ("McLove", a compile time string, e.g., aconst char*) to an array (achar[]), which won’t work.To copy the data
"McLove"into the array, you need to usestrncpy:Or better yet, use
std::string:And now that will work, because
std::stringhas anoperator=that works withconst char*.As a side note, don’t needlessly allocate objects on the free store (using
new); allocate them on the stack: