This is my code:
typedef struct{
char name[64];
} Cat;
Cat createCat(char name[64]) {
Cat newCat;
newCat.name = name;
return newCat;
}
It compiles with the following error message:
incompatible types when assigning to type ‘char[64]’ from type ‘char
*’
What am I doing wrong here?
Array decay to pointers when passed to functions. So:
is the same as:
and the line:
is attempting to assign a
char*to achar[], as the error states. As Mystical has already commented, you need to usememcpy()orstrcpy()(orstrncpy()) to copynametonewCat.name. If you usememcpy()you must remember to null terminatenewCat.name.