求助:关于函数中return0的问题
刚学习C不久,自己写了个动态单行链表的程序,于是对return更加茫然了,求解答,求喷,求打击代码:
# include <stdio.h>
# include <malloc.h>
# define null 0
struct Student
{
char name[20];
int age;
float score;
struct Student * pnext;
};
struct Student * creat(void)
{
int i,n;
struct Student * p2;
printf("请输入学生人数:\n");
scanf("%d",&n);
struct Student * phead = (struct Student *)malloc(sizeof(struct Student));
if (null == phead)
{
printf("内存分配失败!退出程序\n");
return 0; //函数的返回类型是struct Student * ,这里为什么返回0是正确的?
} //而换成了其他整数报错?# include <stdlib.h> 头文件中的exit()
//是否是强制退出函数?
phead->pnext = null;
p2 = phead;
p2->pnext = null;
for (i=0;i<n;i++)
{
struct Student * p1 = (struct Student *)malloc(sizeof(struct Student));
if (null == p1)
{
printf("内存分配失败!退出程序\n");
return 0;
}
printf("请输入第%d个学生的名字:\n",i+1);
scanf("%s",p1->name);
printf("请输入第%d个学生的年纪:\n",i+1);
scanf("%d",&p1->age);
printf("请输入第%d个学生的成绩:\n",i+1);
scanf("%f",&p1->score);
p2->pnext = p1;
p1->pnext = null;
p2 = p1;
}
p2->pnext = null;
return phead;
}
int main(void)
{
struct Student * phead = null;
phead = creat();
return 0;
}