vaghxc 2023-12-17 18:52 采纳率: 88.2%
浏览 3
已结题

求答疑 输出结果不对,只输出一部分

想知道输出结果为什么只有1,旁边的调试框中的name为什么是等于117‘g’呢??还有结构体的int score[3]是让我输入三个成绩的意思吗?也就是每个人有三门课的成绩

img

img

#include<stdio.h>
struct grade
{
    int num;
    char name;
    int score[3];
};
struct grade student[5];
char print(struct grade student[],int a) 
{
    printf("输出结果为:\n");
    for(int i=0;i<a;i++)
    {
        printf("%d ",student[i].num);
        printf("%s",student[i].name);
        printf("%d %d %d",student[i].score[i],student[i].score[i+1],student[i].score[i+2]);
        printf("\n");
    }
}
int main()
{
    printf("请输入记录:");
    for(int i=0;i<5;i++)
    {
        scanf("%d",&student[i].num);
        scanf("%s",&student[i].name);
        scanf("%d",&student[i].score[i]);
        scanf("%d",&student[i].score[i+1]);
        scanf("%d",&student[i].score[i+2]);
    }
    print(student,5);
    return 0;
}


  • 写回答

2条回答 默认 最新

  • 柯本 2023-12-17 19:22
    关注

    u表示unicode ,这个与你的编译运行环境有关,而int score[3]是你自己写的,没有注释只有作者才知道它的真正意义,别人都是要靠猜的,score英文是成绩的意义,那么int score[3]应该就是3门课的成绩
    你的程序的问题是,明显的下标溢出了,你的score[i],score[i+2],score[i+3]肯定都是错的,score的下标只能是0/1/2,当i>=3时,它下标溢出了
    若只是输入/出,改好的

    #include<stdio.h>
    struct grade
    {
        int num;
        char name;
        int score[3];
    };
    struct grade student[5];
    char print(struct grade student[],int a) 
    {
        printf("输出结果为:\n");
        for(int i=0;i<a;i++)
        {
            printf("%d ",student[i].num);
            printf("%s",student[i].name);
            printf("%d %d %d",student[i].score[0],student[i].score[1],student[i].score[2]);
            printf("\n");
        }
    }
    int main()
    {
        printf("请输入记录:");
        for(int i=0;i<5;i++)
        {
            scanf("%d",&student[i].num);
            scanf("%s",student[i].name); //字串不用&的
            scanf("%d",&student[i].score[0]);
            scanf("%d",&student[i].score[1]);
            scanf("%d",&student[i].score[2]);
        }
        print(student,5);
        return 0;
    }
     
     
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月25日
  • 已采纳回答 12月17日
  • 创建了问题 12月17日

悬赏问题

  • ¥100 求懂行的大ge给小di解答下!
  • ¥15 pcl运行在qt msvc2019环境运行效率低于visual studio 2019
  • ¥15 MAUI,Zxing扫码,华为手机没反应。可提高悬赏
  • ¥15 python运行报错 ModuleNotFoundError: No module named 'torch'
  • ¥100 华为手机私有App后台保活
  • ¥15 sqlserver中加密的密码字段查询问题
  • ¥20 有谁能看看我coe文件到底哪儿有问题吗?
  • ¥20 我的这个coe文件到底哪儿出问题了
  • ¥15 matlab使用自定义函数时一直报错输入参数过多
  • ¥15 设计一个温度闭环控制系统