qq_45065977 2019-05-22 16:03 采纳率: 0%
浏览 341

萌新求问,关于C语言函数返回值的问题

有没有大佬解释一下为什么第一段程序改成第二段程序运行就出错啊,跪谢。
一:
#include
#include
#include

typedef struct student{
char size[6];
float score;
struct student* next;
} Student;

int main()
{
int n=1;
char input[7];
Student *head=NULL;
Student *current, *end;
printf("please input the size of the student No.%d\n",n);
while(gets(input)!=NULL && input[0]!='0')
{
current=(Student *)malloc(sizeof(Student));
if(head==NULL)
{
head=current;
} else {
end->next=current;
}
printf("please input the score of the student No.%d\n",n);
strcpy(current->size,input);
scanf("%f",&current->score);
while(getchar()!='\n')
{
continue;
}
current->next=NULL;
n++;
printf("please input the size of the student No.%d\n",n);
end=current;
}

Student *headl=NULL;
headl=head;
if(headl==NULL)
{
    printf("No list entered!!!!!!!!\n");
}
while( headl!=NULL )
{
    printf("the student with a school number of %6s have a score of %4.2f\n",headl->size,headl->score);
    headl=headl->next;
}

while( headl!=NULL )
{
    free(headl);
    headl->next=headl;
} 
return 0;   

}

二:
#include
#include
#include

typedef struct student{
char size[6];
float score;
struct student* next;
} Student;

Student *add( Student *head );
void printfl( Student *head );
void freel( Student *head );

int main()
{
Student *head=NULL, *headl=NULL;
head=add(head);
printfl(head);
free(head);
return 0;
}

Student *add( Student *head )
{
int n=1;
char input[7];
Student *headl=NULL;
headl=head;
Student *current, *end;
printf("please input the size of the student No.%d\n",n);
while(gets(input)!=NULL && input[0]!='0')
{
if(headl==NULL)
{
headl=current;
} else {
end->next=current;
}
current=(Student *)malloc(sizeof(Student));
printf("please input the score of the student No.%d\n",n);
strcpy(current->size,input);
current->next=NULL;
scanf("%f",&current->score);
while(getchar()!='\n')
{
continue;
}
n++;
printf("please input the size of the student No.%d\n",n);
end=current;
}
return headl;
}

void printfl( Student *head )
{
Student *headl=NULL;
headl=head;
if(headl==NULL)
{
printf("No list entered!!!!!!!!\n");
}
while( headl!=NULL )
{
printf("the student with a school number of %6s have a score of %f\n",headl->size,headl->score);
headl=headl->next;
}
}

void freel( Student *head )
{
Student *headl=NULL;
headl=head;
while( headl!=NULL )
{
free(headl);
headl->next=headl;
}
}

  • 写回答

2条回答

  • 普通网友 2019-05-22 16:13
    关注

    可以单步debug啊,这样你就知道出错的信息和出错的位置了。

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧