C struct problem -
i trying learn structs in c, not understand why cannot assign title example:
#include <stdio.h> struct book_information { char title[100]; int year; int page_count; }my_library; main() { my_library.title = "book title"; // problem here, 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; }
error message:
books.c: in function ‘main’: books.c:13: error: incompatible types when assigning type ‘char[100]’ type ‘char *’
lhs array, rhs pointer. need use strcpy
put pointed-to bytes array.
strcpy(my_library.title, "book title");
take care not copy source data > 99 bytes long here need space string-terminating null ('\0') character.
the compiler trying tell wrong in detail:
error: incompatible types when assigning type ‘char[100]’ type ‘char *’
look @ original code again , see if makes more sense now.
Comments
Post a Comment