萌新求问,关于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个回答

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

你可以在你修改过的代码附近打个断点,或者分段打点找出出问题的代码段,然后在那里单步执行找到具体位置,看下是不是符号打错了之类的问题

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐