鱼C论坛

 找回密码
 立即注册
查看: 1104|回复: 4

[已解决]第四十四课 图书馆作业问题!

[复制链接]
发表于 2020-2-7 17:21:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
各位大佬,我这个程序什么地方写错了啊?
为什么不能用,打印出来都是乱码,不然就是报错...




#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct Date
{
        int  year;
        int month;
        int day;
};

struct Book
{
        char title[128];
        char author[40];
        float price;
        struct Date date;
        char publisher[40];
};
void getInput(struct Book* book);
void getInput(struct Book* book) {
        printf("请输入书名: ");
        scanf_s("%s", book->title, 128);
        printf("请输入作者: ");
        scanf_s("%s", book->author, 40);
        printf("请输入售价: ");
        scanf_s("%f", &book->price);
        printf("请输入出版日期: ");
        scanf_s("%d-%d-%d", &book->date.year, &book->date.month, &book->date.day);
        printf("请输入出版社: ");
        scanf_s("%s", book->publisher, 40);
};
void printfBook(struct Book* book);
void printfBook(struct Book* book)
{
        printf("书名: %s\n", book -> title);
        printf("作者: %s\n", book->author);
        printf("售价: %.2f\n", book->price);
        printf("出版日期: %d-%d-%d\n", book->date.year, book->date.month, book->date.day);
        printf("出版社: %s\n", book->publisher);
}
int main(void)
{
        struct Book *tsg[10];
        struct Book *tsg1;
        int num = 0;
        int count = 0;
        do
        {
                count++;
                tsg1 = (struct Book *)malloc(sizeof(struct Book));
                if (tsg == NULL)
                {
                        printf("存储出错!\n");
                        exit(1);
                }
                printf("请录入第%d本书的信息...\n",count);
                getInput(tsg1);
                putchar('\n');
                printf("如果结束请输入1");
                scanf_s("%d", &num);
                tsg[count] = tsg1;
                free(tsg1);
        } while (num != 10);
        int i;
        printf("\n\n录入完毕,现在开始打印验证...\n\n");
        for (i = 0; i < count; i++)
        {
                printf("打印第%d本书的信息...\n",i+1);
                printfBook(tsg[i]);
                putchar('\n');
        }
        return 0;
}
最佳答案
2020-2-7 19:11:17
1,tsg[count] = tsg1;错了,应该为tsg[count-1] = tsg1;2,free(tsg1);这句话不要。如果释放了,那你输入的数据就没了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-7 19:11:17 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
1,tsg[count] = tsg1;错了,应该为tsg[count-1] = tsg1;2,free(tsg1);这句话不要。如果释放了,那你输入的数据就没了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-7 20:11:25 | 显示全部楼层
召唤风云 发表于 2020-2-7 19:11
1,tsg[count] = tsg1;错了,应该为tsg[count-1] = tsg1;2,free(tsg1);这句话不要。如果释放了,那你输入的 ...


谢谢大佬指点.
第一个我明白了.
第二个我想的是在每一次循环中,给tsg赋值完了,就把stg1释放了,在下一次循环的时候在从新申请动态空间,这样不行吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-7 20:38:36 From FishC Mobile | 显示全部楼层
你理解错了释放的意思。它的意思是将你要释放的那个指针所指向的变量清除掉。也就是说你写进去的数据被释放掉了。你只是想让指针指向一个新的 变量,根本就不需要释放。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-8 10:36:11 | 显示全部楼层
召唤风云 发表于 2020-2-7 20:38
你理解错了释放的意思。它的意思是将你要释放的那个指针所指向的变量清除掉。也就是说你写进去的数据被释放 ...

谢谢,我把释放的删除后就正常了.
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 02:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表