Incompatible types when assigning to type char[] f

2019-08-17 16:14发布

问题:

let say I have this struct

 struct person {
   char last_name [10];
 };
typedef struct person Person;

And I fill this struct-object with a certain name

Person p;
Person *ptrPerson = &p;
strcpy(ptrPerson->last_name, "Johnson"); 

And then I put this name in an array of type Person ... put in the first position

Person queue[10];
queue[0] = *ptrPerson;

So far so good. But how do I nullify the arraypostion after that - or at least put a character "-" there instead:

queue[0].last_name = "-";

I get the following compilation error:

error: incompatible types when assigning to type 'char[10]' from type 'int'

回答1:

queue[0].last_name = "-"; is trying to assign the address returned by "-".

Use strcpy (queue[0].last_name ,"-");



回答2:

Either keep track of which entries are used and which are "free". Or if you want to clear the whole structure you can use e.g. memset. Or to copy a string you already are using strcpy at one place, why don't you use it at the other?



标签: c struct