| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 677 人关注过本帖
标题:我不知道怎么改,各位大侠帮我!
只看楼主 加入收藏
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
结帖率:83.33%
收藏
已结贴  问题点数:10 回复次数:4 
我不知道怎么改,各位大侠帮我!
#include<stdio.h>
#include<malloc.h>
struct student
{
    int num;
    struct student *link;
}stud;
  
 creat(int n)
{
    struct student *h,*p;
    int i;
    if((h=(stud *)malloc(sizeof(stud)))==NULL);
    {
        printf("not enough memory!");
        return(0);
    }
    h=NULL;
    for(i=0;i<=n;i++)
    {
         p=h;
         
        if((p=(stud *)malloc(sizeof(stud)))==NULL);

            
            
        {
            printf("内存不足!");
            return(0);
        }
        scanf("%d",&p->num);
        p->link=h;
        h=p;
    }
    }
    void main()
    {
        struct student *h;
        h=(struct student *)creat(4);
    }
   
搜索更多相关主题的帖子: 内存 include memory return 
2011-09-24 09:53
ppfly
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:297
专家分:1956
注 册:2009-5-17
收藏
得分:3 
简单修改下:
注意creat函数要有返回值,if结尾没有分号,动态分配malloc的用法。
程序代码:
#include<stdio.h>
#include<malloc.h>
struct student
{
    int num;
    struct student *link;
}stud;

struct student* creat(int n)
{
    struct student *h,*p;
    int i;
    if((h= (struct student *)malloc(sizeof(stud)))==NULL )
    {
        printf("not enough memory!");
        return(0);
    }
    for(i=0;i<=n;i++)
    {
        p=h;
        if((p=(struct student *)malloc(sizeof(stud)))==NULL)
        {
            printf("内存不足!");
            return(0);
        }
        scanf("%d",&p->num);
        p->link=h;
        h=p;
    }
    return h;
}
void print(struct student* h)
{
    struct student* p=h;
    printf("%d",p->num);
    p=p->link;
    while(p!=NULL)
    {
        printf("->%d",p->num);
        p=p->link;
    }
    printf("\n");
}
int main()
{
    struct student *h;
    h=creat(4);
    print(h);
    return 0;
}

 

********多贴代码,少说空话*******
2011-09-24 10:27
拾忆
Rank: 2
等 级:论坛游民
帖 子:23
专家分:63
注 册:2011-9-23
收藏
得分:3 
你的程序有两个不该犯的错误!
一,IF 后面不应有分号;
二, if((h=(stud *)malloc(sizeof(stud)))==NULL);这句应改为if((h=(student *)malloc(sizeof(stud)))==NULL)
     后面相同之处都要改!
三,creat函数creat(int n)没有遵循NASI C 标准,应改为int creat(int n)
    否则在不同平台下可能会报错!
这是我改好的:

#include<stdio.h>
#include<malloc.h>
struct student
{
    int num;
    struct student *link;
}stud;
int create(int n)
{    struct student *h;struct student *p;
    int i;
    if((h=(student *)malloc(sizeof(stud)))==NULL)
    {    printf("not enough memory!");
        return(0);
    }
    h=(student *)NULL;
    for(i=0;i<=n;i++)
    {    p=h;
        if((p=(student *)malloc(sizeof(stud)))==NULL)
        {    printf("内存不足!");
            return(0);
        }
        scanf("%d",&p->num);
        p->link=h;
        h=p;
    }
}
void main()
{    struct student *h;
    h=(struct student *)create(4);
}

2011-09-24 12:41
杜撰
Rank: 2
来 自:北京
等 级:论坛游民
帖 子:53
专家分:69
注 册:2011-5-14
收藏
得分:3 
#include<stdio.h>
#include<malloc.h>
struct student
{
    int num;
    struct student *link;
}stud;
int create(int n)
{    struct student *h;
     struct student *p;
     int i;
     if((h=(struct student *)malloc(sizeof(stud)))==NULL)
    {    printf("not enough memory!");
        return(0);
    }
    h=(struct student *)NULL;
    for(i=0;i<=n;i++)
    {    p=h;
        if((p=(struct student *)malloc(sizeof(stud)))==NULL)
        {    printf("内存不足!");
            return(0);
        }
        scanf("%d",&p->num);
        p->link=h;
        h=p;
    }
}
void main()
{    struct student *h;
    h=(struct student *)create(4);
}

if((h=(student *)malloc(sizeof(stud)))==NULL) 改为 if((p=(struct student *)malloc(sizeof(stud)))==NULL),否则某些编译器不通过。

以下几处同理

我的青春我做主,奋斗!
2011-09-24 17:46
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
收藏
得分:0 
原来这样,明白了
2011-09-24 18:14
快速回复:我不知道怎么改,各位大侠帮我!
数据加载中...
 
   



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

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