回 帖 发 新 帖 刷新版面

主题:[讨论]关于C语言链表的一个问题

#include <stdio.h>\\要输入三个学生的信息,学号和分数
#include <stdlib.h>
#include <malloc.h>
#define len sizeof(struct student)
int main ()
{  
    int i;
    struct student { int xuehao;int score;struct student *next;}stu[3];
    struct student *head, *p1, *p2;
    for(i=0;i<3;i++)
    {
        p1=(struct student *)malloc(len);
        if(i==0)
            head=p1=p2;  
        else
            p2->next=p1;    
        p2=p1;
        printf("please input numble: \n");
        scanf("%d",&p1->xuehao);
        printf("qing shu ru di er ge shu :\n");
        scanf("%d",& p1->score);
    }
       p2->next=NULL;
       p2=head;
    while(p2->next!=NULL)
    {
        printf("%d %d\n",p2->xuehao,p2->score);
        p2=p2->next;
    }

return 0;
}
为什么这里无法运行啊,我只要输入一个数字就会提示错误啊,请教高手指点啊

回复列表 (共2个回复)

沙发

建议宏别这么定义,#define len sizeof(struct ...)
这里有个问题。head=p1=p2
改为head = p2 = p1;再好好看看吧。。

板凳

typedef struct
{
...
} Student;
inline size_t Get_Len(void)
{
  return sizeof(Student);
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册