Consider this code segment:
char message[255];
char newMessage[255];
int i;
for (i = 0 ; i < 255 ; i++)
message[i] = i;
newMessage = message;
When I try to do this I get for the last line an error:
incompatible types when assigning to type ‘char[255]’ from type ‘char *
Why do I get that if the arrays has the same type? How do I fix it?
Thanks in advance
One way to fix this is by declaring
newMessageas a pointer:char* newMessage.Another is by using
memcpy()— orstrncpy()ifmessageis a string — to copy the contents ofmessageintonewMessage.Which method is preferable depends on what you then do with
newMessage.