bodyHealthy 2021-12-18 20:10 采纳率: 80%
浏览 27
已结题

建立静态链表,接收不到用户输入的第一组数据


#include<stdio.h>

void input();
void print();

struct student{
    int number;
    int score;
    struct student *next;
}stu1,stu2,stu3;

int i;

int main()
{
    struct student *head;
    head=&stu1;
    stu1.next=&stu2;
    stu2.next=&stu3;
    stu3.next=NULL;
    struct student *n;
    n=head;
    input(*n);
    print(*n);
    return 0;
}

void input(struct student *a)
{
    printf("Please enter the information of students:\n");
    for(i=0;i<3;i++){
        scanf("%d %d",&(*a).number,&(*a).score);
        a=(*a).next;
    }
}

void print(struct student *p)
{
    printf("\nThe information of them is:\n");
    for(i=0;i<3;i++){
        printf("%d %d\n",(*p).number,(*p).score);
        p=(*p).next;
    }
}

为什么stu1接收不到输入的第一组数据?打印结果第一组数据为0 0,另外两组的数据都正常。

  • 写回答

1条回答 默认 最新

  • qzjhjxj 2021-12-18 21:11
    关注

    供参考:

    #include<stdio.h>
    void input();
    void print();
    struct student{
        int number;
        int score;
        struct student *next;
    }stu1,stu2,stu3;
    int i;
    void input(struct student *a); //修改
    void print(struct student *p); //修改
    int main()
    {
        struct student *head;
        head=&stu1;
        stu1.next=&stu2;
        stu2.next=&stu3;
        stu3.next=NULL;
        struct student *n;
        n=head;
        input(n);//input(*n); 修改
        print(n);//print(*n);
        system("pause");
        return 0;
    }
    void input(struct student *a)
    {
        printf("Please enter the information of students:\n");
        for(i=0;i<3;i++){
            scanf("%d %d",&(*a).number,&(*a).score);
            a=(*a).next;
        }
    }
    void print(struct student *p)
    {
        printf("\nThe information of them is:\n");
        for(i=0;i<3;i++){
            printf("%d %d\n",(*p).number,(*p).score);
            p=(*p).next;
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

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

悬赏问题

  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化