大神们,小弟想知道为什么我调用output之后不能输出所有学生的学号?[face]monkey2:0

大神们,小弟想知道为什么我调用output之后不能输出所有学生的学号?[face]monkey2:019.png[/face] #include#include#include#define New_node (stu*)malloc(sizeof(stu)) typedef struct student { int Student_ID; struct student *next; }stu; void input(stu*R) //输入学生信息 { stu *p=New_node; //p作为新节点存放p的数据 printf("\n请输入十位数的学号:\n"); scanf("%d",&p->Student_ID); p->next=NULL; R->next=p; R=p; } void output(stu *L) { stu *p; p=L->next; while(p!=NULL) { printf("%d\n",p->Student_ID); p=p->next; } } main() { stu *L=New_node; L->next=NULL; //建立头节点 stu *R=L; //尾指针指向头节点 while(1) { int i; printf("请输入选择\n"); scanf("%d",&i); switch(i) { case 1:input(R); case 2:output(L); } } }

input 参数改成二级指针,stu **R,

你传入一个一级指针,仅仅只是改变了input函数内的R,这个R是一个复制品,并不是外面R的本身。

所以你需要传 stu **R,这样才能是*R = p 有意义。