鱼C论坛

 找回密码
 立即注册
查看: 873|回复: 3

[已解决]为什么这个在储存第二个人的数据时会报错啊??

[复制链接]
发表于 2023-8-12 13:10:28 | 显示全部楼层 |阅读模式

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

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

x
为什么这个在储存第二个人的数据时会报错啊??

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

#define LEN sizeof(struct cesh)

struct cesh* creat();
void print(struct cesh*head);
int i;

void main()
{
        struct cesh* stu;
        stu = creat();
        print(stu);

        system("pause");
}

struct cesh
{
        int xuehao;
        double score;
        struct cesh* next;
};

struct cesh* creat()
{
        struct cesh *p1,*p2,*head;
        p1 = p2 = (struct cesh*)malloc(LEN);
        printf("输入学号。\n");
        scanf_s("%d", &p1->xuehao);
        printf("输入成绩。\n");
        scanf_s("%lf", &p1->score);
        head = NULL;
        i = 0;
        while (p1->xuehao)
        {
                i++;
                if (i == 1)
                {
                        head = p1;
                }

                else
                {
                        p2->next = p1;
                }
                p2 = p1;
                p1 = (struct cesh*)malloc(LEN);
                printf("输入学号\n");
                scanf_s("%d",p1->xuehao);
                printf("输入成绩\n");
                scanf_s("%lf",p1->score);
        }
        p2->next = NULL;
        return head;
}

void print(struct cesh* head)
{
        struct cesh* p;
        printf("\n这里一共有 %d 个数据\n\n", i);
        p = head;
        if (head)
        {
                do
                {
                        printf("学号为 %d 的成绩为 %0.2f.\n", p->xuehao, p->score);
                        p = p->next;
                } while (p);
        }
}
最佳答案
2023-8-12 13:10:36
问题出在以下这行代码:
scanf_s(\%d\, p1->xuehao);
以及
scanf_s(\%lf\, p1->score);
在这里,你应该传递变量的地址给scanf_s函数,而不是变量的值。所以需要修改为:
scanf_s(\%d\, &(p1->xuehao));
以及
scanf_s(\%lf\, &(p1->score));
修改后的完整代码如下所示:
#include <stdio.h>#include <malloc.h>#include <stdlib.h>#define LEN sizeof(struct cesh)struct cesh* creat();void print(struct cesh* head);int i;int main() {    struct cesh* stu;    stu = creat();    print(stu);    system(\pause\);    return 0;}struct cesh {    int xuehao;    double score;    struct cesh* next;};struct cesh* creat() {    struct cesh* p1, * p2, * head;    p1 = p2 = (struct cesh*)malloc(LEN);    printf(\输入学号。\\);    scanf_s(\%d\, &(p1->xuehao));    printf(\输入成绩。\\);    scanf_s(\%lf\, &(p1->score));    head = NULL;    i = 0;    while (p1->xuehao) {        i++;        if (i == 1) {            head = p1;        }        else {            p2->next = p1;        }        p2 = p1;        p1 = (struct cesh*)malloc(LEN);        printf(\输入学号\\);        scanf_s(\%d\, &(p1->xuehao));        printf(\输入成绩\\);        scanf_s(\%lf\, &(p1->score));    }    p2->next = NULL;    return head;}void print(struct cesh* head) {    struct cesh* p;    printf(\\这里一共有 %d 个数据\\\, i);    p = head;    if (head) {        do {            printf(\学号为 %d 的成绩为 %0.2f.\\, p->xuehao, p->score);            p = p->next;        } while (p);    }}
这样修改后,你应该能够正确地储存第二个人的数据了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-8-12 13:10:36 | 显示全部楼层    本楼为最佳答案   
问题出在以下这行代码:
scanf_s(\%d\, p1->xuehao);
以及
scanf_s(\%lf\, p1->score);
在这里,你应该传递变量的地址给scanf_s函数,而不是变量的值。所以需要修改为:
scanf_s(\%d\, &(p1->xuehao));
以及
scanf_s(\%lf\, &(p1->score));
修改后的完整代码如下所示:
#include <stdio.h>#include <malloc.h>#include <stdlib.h>#define LEN sizeof(struct cesh)struct cesh* creat();void print(struct cesh* head);int i;int main() {    struct cesh* stu;    stu = creat();    print(stu);    system(\pause\);    return 0;}struct cesh {    int xuehao;    double score;    struct cesh* next;};struct cesh* creat() {    struct cesh* p1, * p2, * head;    p1 = p2 = (struct cesh*)malloc(LEN);    printf(\输入学号。\\);    scanf_s(\%d\, &(p1->xuehao));    printf(\输入成绩。\\);    scanf_s(\%lf\, &(p1->score));    head = NULL;    i = 0;    while (p1->xuehao) {        i++;        if (i == 1) {            head = p1;        }        else {            p2->next = p1;        }        p2 = p1;        p1 = (struct cesh*)malloc(LEN);        printf(\输入学号\\);        scanf_s(\%d\, &(p1->xuehao));        printf(\输入成绩\\);        scanf_s(\%lf\, &(p1->score));    }    p2->next = NULL;    return head;}void print(struct cesh* head) {    struct cesh* p;    printf(\\这里一共有 %d 个数据\\\, i);    p = head;    if (head) {        do {            printf(\学号为 %d 的成绩为 %0.2f.\\, p->xuehao, p->score);            p = p->next;        } while (p);    }}
这样修改后,你应该能够正确地储存第二个人的数据了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-12 13:11:14 | 显示全部楼层
这段代码在储存第二个人的数据时会报错是因为在输入学号和成绩时,使用了错误的scanf_s函数的调用方式。

scanf_s函数需要传入参数的地址,而不是参数本身。所以在以下两行代码中:

scanf_s("%d",p1->xuehao);
scanf_s("%lf",p1->score);

应该改为:

scanf_s("%d", &p1->xuehao);
scanf_s("%lf", &p1->score);

修改后的代码如下:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

#define LEN sizeof(struct cesh)

struct cesh* creat();
void print(struct cesh*head);
int i;

void main()
{
    struct cesh* stu;
    stu = creat();
    print(stu);

    system("pause");
}

struct cesh
{
    int xuehao;
    double score;
    struct cesh* next;
};

struct cesh* creat()
{
    struct cesh *p1,*p2,*head;
    p1 = p2 = (struct cesh*)malloc(LEN);
    printf("输入学号。\n");
    scanf_s("%d", &p1->xuehao);
    printf("输入成绩。\n");
    scanf_s("%lf", &p1->score);
    head = NULL;
    i = 0;
    while (p1->xuehao)
    {
        i++;
        if (i == 1)
        {
            head = p1;
        }
        else
        {
            p2->next = p1;
        }
        p2 = p1;
        p1 = (struct cesh*)malloc(LEN);
        printf("输入学号\n");
        scanf_s("%d", &p1->xuehao);
        printf("输入成绩\n");
        scanf_s("%lf", &p1->score);
    }
    p2->next = NULL;
    return head;
}

void print(struct cesh* head)
{
    struct cesh* p;
    printf("\n这里一共有 %d 个数据\n\n", i);
    p = head;
    if (head)
    {
        do
        {
            printf("学号为 %d 的成绩为 %0.2f.\n", p->xuehao, p->score);
            p = p->next;
        } while (p);
    }
}

这样修改后,代码应该能够正常运行并储存第二个人的数据。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-12 13:11:27 | 显示全部楼层
根据您提供的代码,我发现在存储第二个人的数据时会报错的原因是在两处输入语句中存在错误。以下是您的代码中需要进行修正的地方:

1. 在 creat() 函数中,下面这两行代码中的 scanf_s 函数调用不正确:

   printf("输入学号\n");
   scanf_s("%d", p1->xuehao);
   printf("输入成绩\n");
   scanf_s("%lf", p1->score);
   

   应将这两行代码修改为:

   printf("输入学号\n");
   scanf_s("%d", &(p1->xuehao));
   printf("输入成绩\n");
   scanf_s("%lf", &(p1->score));
   

   scanf_s 函数需要传递变量的地址作为参数,而 p1->xuehaop1->score 分别是变量 xuehaoscore 的值,所以需要使用 & 运算符获取它们的地址。

2. 在 main() 函数中,void main() 应改为 int main(),并在函数的末尾添加 return 0;

修改后的代码如下:
#include <stdio.h>
#include <stdlib.h>

#define LEN sizeof(struct cesh)

struct cesh {
    int xuehao;
    double score;
    struct cesh* next;
};

struct cesh* creat();
void print(struct cesh* head);

int i;

int main() {
    struct cesh* stu;
    stu = creat();
    print(stu);
    system("pause");
    return 0;
}

struct cesh* creat() {
    struct cesh* p1, * p2, * head;
    p1 = p2 = (struct cesh*)malloc(LEN);
    printf("输入学号。\n");
    scanf_s("%d", &(p1->xuehao));
    printf("输入成绩。\n");
    scanf_s("%lf", &(p1->score));
    head = NULL;
    i = 0;
    while (p1->xuehao) {
        i++;
        if (i == 1) {
            head = p1;
        }
        else {
            p2->next = p1;
        }
        p2 = p1;
        p1 = (struct cesh*)malloc(LEN);
        printf("输入学号\n");
        scanf_s("%d", &(p1->xuehao));
        printf("输入成绩\n");
        scanf_s("%lf", &(p1->score));
    }
    p2->next = NULL;
    return head;
}

void print(struct cesh* head) {
    struct cesh* p;
    printf("\n这里一共有 %d 个数据\n\n", i);
    p = head;
    if (head) {
        do {
            printf("学号为 %d 的成绩为 %0.2f.\n", p->xuehao, p->score);
            p = p->next;
        } while (p);
    }
}

修改后的代码应该可以正常地存储多个人的数据并输出结果。如果您遇到其他问题,请随时提问。
如果问题已经解决,请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-6 08:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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