鱼C论坛

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

[已解决]请问各位这段代码是哪里出问题了呀,出现段错误的提示

[复制链接]
发表于 2020-9-13 13:23:11 | 显示全部楼层 |阅读模式

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

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

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

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

struct Book
{
    char title[20];
    char author[10];
    float price;
    struct Date date;
    char publisher[20];
};

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[n];

    printf("Now start save book\n");
    for (int i = 0; i < n; i++)
    {
        getSize(library[i]);
        getInput(library[i]);
    }

    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[i]);
    }

    for (int i = 0; i < n; i++)
    {
        freeLibrary(library[i]);
    }
}
最佳答案
2020-9-13 14:39:39
本帖最后由 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[20];
    char author[10];
    float price;
    struct Date date;
    char publisher[20];
};

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[n];  //这里定义成结构体数组

    printf("Now start save book\n");
    for (int i = 0; i < n; i++)
    {
        getSize(&library[i]);//这里实参为地址,下面几个函数调用时,实参也一样
        getInput(&library[i]);
    }

    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[i]);
    }

    for (int i = n; i >0; i++)
    {
        freeLibrary(&library[i]);
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-13 13:44:47 | 显示全部楼层
你可以把那个i 放前边和n一块定义了应该就能运行了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-13 14:08:57 | 显示全部楼层
一枚丶学渣 发表于 2020-9-13 13:44
你可以把那个i 放前边和n一块定义了应该就能运行了

能运行,而且也不报错,可是运行到输入作者时就提示段错误,核心转存&#128579;
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-13 14:39:39 | 显示全部楼层    本楼为最佳答案   
本帖最后由 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[20];
    char author[10];
    float price;
    struct Date date;
    char publisher[20];
};

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[n];  //这里定义成结构体数组

    printf("Now start save book\n");
    for (int i = 0; i < n; i++)
    {
        getSize(&library[i]);//这里实参为地址,下面几个函数调用时,实参也一样
        getInput(&library[i]);
    }

    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[i]);
    }

    for (int i = n; i >0; i++)
    {
        freeLibrary(&library[i]);
    }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-13 16:03:40 | 显示全部楼层
sunrise085 发表于 2020-9-13 14:39
你在main中定义的是结构体指针数组,然后给指针分配空间,这样是有问题的,调用函数getSize动态分配给指针 ...

谢谢老哥
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-1-13 06:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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