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啊,这样你就知道出错的信息和出错的位置了。

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型