February 24th, 2013, 04:51 AM
Transfer data to a pointer of a struct.
could you please help,
I get this error while trying to transfer data to a pointer of a struct.
incompatible types when assigning to type ‘char’ from type ‘char
typedef struct PhoneBook
void AddEntry(phone *phonebook_ptr)
February 24th, 2013, 04:54 AM
February 24th, 2013, 12:20 PM
More specifically, you are not copying the string A into your struct with that assignment statement, but rather you are attempting a damned illegal thing: assigning a pointer value to an array name. That is why you got that error message.
Now, if you had declared Name to be a char pointer, you would not have gotten that error message, but you would still have made an enormous mistake. Because in that case, you would have assigned a char pointer, the address of A, to that char pointer in that struct and when A goes away when you leave that function then your struct will be pointing to garbage. And even if you made A a global or a static, then every time you called that function to enter a different name into a different struct, all structs' Name fields would immediately change to the new name input.
Instead, you need to copy the contents of A into Name. You need to use strcpy().