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

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 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存