qq_36674149
一股清风
2020-06-25 10:42
采纳率: 66.7%
浏览 130

c语言编程,程序编译没错,运行弹出debug,问题在排序模块,把主函数的排序注释掉后不会弹出debug

# ## 图片说明

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

void input(struct student * stu, int val);
void paixu(struct student * stu, int val);
void output(struct student * stu, int val);

struct student 
{
    int age;
    char name[100];
    float score;
};

int main(void)
{
    int val;
    printf("请输入学生总数:\n");
    scanf("%d", &val);
    float a = 3.0/10;

    struct student * stu = (struct student *)malloc(val*sizeof(struct student *));//分配val个struct student *型动态内存

    input(stu, val);//调用输入学生数据的函数

    paixu(stu, val);//调用按成绩排序的函数

    output(stu, val);//调用输出学生数据的函数

    free(stu);//释放学生数据所占用的内存

    return 0;
}


void input(struct student *stu, int val)
{
    int i;

    for(i = 0; i < val; ++i)
    {
        printf("请输入第%d个学生的信息\n",i+1);

        printf("年龄:");
        scanf("%d", &stu[i].age);

        printf("姓名:");
        scanf("%s", &stu[i].name);

        printf("分数:");
        scanf("%f",&stu[i].score);
    }
    return;
}

void paixu(struct student *stu, int val)
{
    int i;
    int j;
    struct student st;

    for(i = 0; i < val-1; ++i)//按成绩升序排序
    {
        for(j = 0; j < val-1-i; ++j)

            if(stu[j].score > stu[j+1].score)
            {
                st = stu[j];
                stu[j] = stu[j+1];
                stu[j+1] = st;
            }

    }
    return;
}

void output(struct student *stu, int val)
{
    int i;

    for(i = 0; i < val; ++i)
    {
        printf("第%d个学生的信息为:\n", i+1);

        printf("年龄:%d 姓名:%s 成绩:%f \n", stu[i].age, stu[i].name, stu[i].score);
    }
    return;
}


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • qiu_shaofeng
    qiu_shaofeng 2020-06-25 18:46
    已采纳

    struct student * stu = (struct student *)malloc(val*sizeof(struct student ));分配val个student结构体大小内存

    点赞 评论

相关推荐