请问各位这段代码是哪里出问题了呀,出现段错误的提示
#include <stdio.h>#include <stdlib.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title;
char author;
float price;
struct Date date;
char publisher;
};
void getInput(struct Book* book)
{
int count = 1;
printf("Enter the %d's book name\n", count);
scanf("%s", book->title);
printf("Enter the author name\n");
scanf("%s", book->author);
printf("Enter the price\n");
scanf("%f", &book->price);
printf("Enter the date\n");
scanf("%d %d %d", &book->date.year, &book->date.month, &book->date.day);
printf("Enter the publisher\n");
scanf("%s", book->publisher);
}
void printBook(struct Book* book)
{
printf("Book name: %s\n", book->title);
printf("Book author: %s\n", book->author);
printf("Book price: %.2f\n", book->price);
printf("Book date: %d-%d-%d", book->date.year, book->date.month, book->date.day);
printf("Book publisher: %s", book->publisher);
}
void getSize(struct Book* book)
{
book = (struct Book*)malloc(sizeof(struct Book));
if (book == NULL)
{
printf("Error\n");
exit(1);
}
}
void freeLibrary(struct Book* book)
{
free(book);
}
void main()
{
int n;
printf("How many books do you want to save?\n");
scanf("%d", &n);
struct Book* library;
printf("Now start save book\n");
for (int i = 0; i < n; i++)
{
getSize(library);
getInput(library);
}
printf("Now list the information about all book\n");
for (int i = 0; i < n; i++)
{
printf("This is the %d's book\n", i + 1);
printBook(library);
}
for (int i = 0; i < n; i++)
{
freeLibrary(library);
}
}
你可以把那个i 放前边和n一块定义了应该就能运行了
一枚丶学渣 发表于 2020-9-13 13:44
你可以把那个i 放前边和n一块定义了应该就能运行了
能运行,而且也不报错,可是运行到输入作者时就提示段错误,核心转存🙃 本帖最后由 sunrise085 于 2020-9-13 14:40 编辑
你在main中定义的是结构体指针数组,然后给指针分配空间,这样是有问题的,调用函数getSize动态分配给指针空间,函数结束后,就无效了。
应该是定义结构体数组,然后给数组内的每个元素动态分配空间
最后释放的时候先释放后面的元素
#include <stdio.h>
#include <stdlib.h>
struct Date
{
int year;
int month;
int day;
};
struct Book
{
char title;
char author;
float price;
struct Date date;
char publisher;
};
void getInput(struct Book* book)
{
int count = 1;
printf("Enter the %d's book name\n", count);
scanf("%s", book->title);
printf("Enter the author name\n");
scanf("%s", book->author);
printf("Enter the price\n");
scanf("%f", &book->price);
printf("Enter the date\n");
scanf("%d %d %d", &book->date.year, &book->date.month, &book->date.day);
printf("Enter the publisher\n");
scanf("%s", book->publisher);
}
void printBook(struct Book* book)
{
printf("Book name: %s\n", book->title);
printf("Book author: %s\n", book->author);
printf("Book price: %.2f\n", book->price);
printf("Book date: %d-%d-%d", book->date.year, book->date.month, book->date.day);
printf("Book publisher: %s", book->publisher);
}
void getSize(struct Book* book)
{
book = (struct Book*)malloc(sizeof(struct Book));
if (book == NULL)
{
printf("Error\n");
exit(1);
}
}
void freeLibrary(struct Book* book)
{
free(book);
}
void main()
{
int n;
printf("How many books do you want to save?\n");
scanf("%d", &n);
struct Book library;//这里定义成结构体数组
printf("Now start save book\n");
for (int i = 0; i < n; i++)
{
getSize(&library);//这里实参为地址,下面几个函数调用时,实参也一样
getInput(&library);
}
printf("Now list the information about all book\n");
for (int i = 0; i < n; i++)
{
printf("This is the %d's book\n", i + 1);
printBook(&library);
}
for (int i = n; i >0; i++)
{
freeLibrary(&library);
}
} sunrise085 发表于 2020-9-13 14:39
你在main中定义的是结构体指针数组,然后给指针分配空间,这样是有问题的,调用函数getSize动态分配给指针 ...
谢谢老哥{:5_109:}
页:
[1]