标题:求助:关于函数中return0的问题
取消只看楼主
solar009
Rank: 2
等 级:论坛游民
帖 子:10
专家分:11
注 册:2011-10-10
结帖率:100%
已结贴  问题点数:20 回复次数:0 
求助:关于函数中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;
}
搜索更多相关主题的帖子: include return null 动态 
2011-11-04 01:03



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-354229-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.252736 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved