我想了解在C中的结构,但我不明白为什么我不能标题指定为我我的例子:C结构问题
#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;
}
错误消息:
books.c: In function ‘main’:
books.c:13: error: incompatible types when assigning to type ‘char[100]’ from type ‘char *’
谢谢,在添加“#include”后,程序编译并按预期工作。我发现的所有例子都使用了scanf或者填充字符串,这对我来说是有意义的。 –
2010-11-16 17:33:19
谢谢彼得。我会考虑'strncpy'的替代品,除非你确定你的输入数据总是足够小以适应。 – 2010-11-16 17:36:45
尽管包含strings.h可能会在您的实现中发生,但您确实应该包含string.h来声明strcpy。 – 2010-11-16 18:26:36