Sto cercando di conoscere le strutture in C, ma io non capisco perché non posso assegnare il titolo come ho il mio esempio:C problema struct
#include <stdio.h>
struct book_information {
char title[100];
int year;
int page_count;
}my_library;
main()
{
my_library.title = "Book Title"; // Problem is here, but why?
my_library.year = 2005;
my_library.page_count = 944;
printf("\nTitle: %s\nYear: %d\nPage count: %d\n", my_library.title, my_library.year, my_library.page_count);
return 0;
}
messaggio di errore:
books.c: In function ‘main’:
books.c:13: error: incompatible types when assigning to type ‘char[100]’ from type ‘char *’
Grazie, dopo aver aggiunto "#include", il programma viene compilato e funziona come previsto. Tutti gli esempi che ho trovato, hanno usato scanf o sono riusciti a riempire le stringhe, e questo per me ha senso. –
Grazie Peter. Prenderò in considerazione le alternative 'strncpy', a meno che tu non sia sicuro che i tuoi dati di input saranno sempre abbastanza piccoli da adattarsi. –
Sebbene comprenda che strings.h può funzionare sulla tua implementazione, dovresti includere string.h per dichiarare strcpy. –