【发布时间】:2016-04-18 07:23:50
【问题描述】:
我的项目是构建书籍结构 - 并用用户参数填充它。
涉及动态分配、数组和指针。
我的book 结构如下:
struct BOOK
{
char* author;
char** genders;
int totalGenders;
char* name;
int* chapterPages;
int totalChapters;
}typedef book;
当我尝试到达作者姓名时,结构中的第 1 行:
struct BOOK
{
char* author;
我没有这样做..我在 main 中的代码:
int main()
{
book* b;
char authorChar[10] = { 0 };
int authorLen;
char* authorName;
// get author name
puts("please enter the name of the author");
scanf("%s", &authorChar);
authorLen = strlen(authorChar);
printf("%d", authorLen); //print to see that lentgh is correct.
authorName = (char*)calloc(authorLen, sizeof(char));
strcpy(authorName, authorChar);
puts("\n");
b->author = authorName;
printf("%d", b->author);
当我调试时,我在这一行遇到了问题:
b->author = authorName;
有什么想法吗? :)
【问题讨论】:
-
scanf("%s", &authorChar);-->scanf("%9s", authorChar);
标签: c pointers struct dynamic-memory-allocation