绅士羊 2023-04-02 22:39 采纳率: 78.6%
浏览 42
已结题

c语言结构体数组指针传参问题

为啥函数接收不到数据啊

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 10
struct Student
{
    char name[20];
    float score[3];
}stu[3];
void Display(struct Student *a)
{
    printf("----信息如下----\n");
    printf("姓名:%s\n", a->name);
    printf("语文:%.2f\n", a->score[0]);
    printf("数学:%.2f\n", a->score[1]);
    printf("英语:%.2f\n", a->score[2]);
    printf("平均成绩:%.2f", (a->score[0] + a->score[1] + a->score[2]) / 3);
}
int main()
{
    int i;
    struct Student *p;
    p = &stu[0];
    for (i = 0; i<3; i++, p++)
    {
        printf("姓名:");
        scanf("%s", &stu[i].name);
        printf("\n");
        printf("语文:");
        scanf("%f", &stu[i].score[0]);
        printf("\n");
        printf("数学:");
        scanf("%f", &stu[i].score[1]);
        printf("\n");
        printf("英语:");
        scanf("%f",& stu[i].score[2]);
    }
    for (i = 0; i<3; i++, p++)
    {
        Display(p);
    }
    system("pause");
    return 0;
}


``
```c
void Display(struct Student a[],int i)
{
 printf("----信息如下----\n");
 printf("姓名:%s\n",a[i].name);
 printf("语文:%.2f\n",a[i].score[0]);
 printf("数学:%.2f\n",a[i].score[1]);
 printf("英语:%.2f\n",a[i].score[2]);
 printf("平均成绩:%.2f",(a[i].score[0]+a[i].score[1]+a[i].score[2])/3);
}

为啥指针传参不对,去掉指针就可以了下面是改的

  • 写回答

3条回答 默认 最新

  • qzjhjxj 2023-04-03 09:50
    关注

    原来代码里,指针 p 已经被 p++ ,已经指向了数组stu[]之外了,修改如下,改动处见注释,供参考:

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #define N 10
    struct Student
    {
        char  name[20];
        float score[3];
    }stu[3];
    void Display(struct Student* a)
    {
        printf("----信息如下----\n");
        printf("姓名:%s\n", a->name);
        printf("语文:%.2f\n", a->score[0]);
        printf("数学:%.2f\n", a->score[1]);
        printf("英语:%.2f\n", a->score[2]);
        printf("平均成绩:%.2f", (a->score[0] + a->score[1] + a->score[2]) / 3);
    }
    void display(struct Student a[], int i)     //修改
    {
        printf("----信息如下----\n");
        printf("姓名:%s\n", a[i].name);
        printf("语文:%.2f\n", a[i].score[0]);
        printf("数学:%.2f\n", a[i].score[1]);
        printf("英语:%.2f\n", a[i].score[2]);
        printf("平均成绩:%.2f", (a[i].score[0] + a[i].score[1] + a[i].score[2]) / 3);
    }
    int main()
    {
        int i;
        struct Student* p;
        //p = &stu[0];       修改 这里没用到指针 p ,多余了
        for (i = 0; i < 3; i++)  //for (i = 0; i < 3; i++, p++) 修改  p++ 多余
        {
            printf("姓名:");
            scanf("%s", stu[i].name);  //scanf("%s", &stu[i].name); 修改
            printf("\n");
            printf("语文:");
            scanf("%f", &stu[i].score[0]);
            printf("\n");
            printf("数学:");
            scanf("%f", &stu[i].score[1]);
            printf("\n");
            printf("英语:");
            scanf("%f", &stu[i].score[2]);
        }
        for (i = 0, p = &stu[0]; i < 3; i++, p++)  // 修改
        {
            Display(p);
            display(stu, i);                       //修改
        }
        system("pause");
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月11日
  • 已采纳回答 4月3日
  • 修改了问题 4月2日
  • 创建了问题 4月2日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵