标题:这个程序怎样改呢?
只看楼主
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
结帖率:83.33%
 问题点数:0 回复次数:18 
这个程序怎样改呢?
#include<stdio.h>
#include<malloc.h>
struct student
{
    int num;
    char name;
    int age;
    int sex;
    struct student *link;
}stud;
  
 creat(int n)
{
    struct student *h,*p;
    int i;
    h=(struct student *)malloc(sizeof(stud));
    if(h==NULL)
    {
        printf("not enough memory!");
        return(0);
    }
    h=NULL;
    for(i=0;i<=n;i++)
    {
         
         
        if((p=(struct student *)malloc(sizeof(stud)))==NULL)

            
            
        {
            printf("内存不足!");
            return(0);
        }
        scanf("%d",&p->num);
        scanf("%s",&p->name);
        scanf("%d",&p->age);
        scanf("%s",&p->sex);
        p->link=h;
        h=p;
    }
     void save(stud *h)
     {
        FILE *fP;
        stud *p;
        if((fp=fopen("d:\\123","w"))==NULL)
        {
            printf("can now open file!");
            exit(1);
        }
        printf("\n saving file...\n");
        p=h;
        while(p!=NULL)
        {
            fwrite(p,sizeof(stud),1,fp);
            p=p->next;
        }
        fclose(fp);
        printf("*******save successful!********\n");
   
   
   
    }
 }
    void main()
    {
        struct student *h;
   
        h=(struct student *)creat(4);
         save(h);
    }
   
搜索更多相关主题的帖子: age include memory return 
2011-09-24 17:15
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
得分:0 
更正一下
#include<stdio.h>
#include<malloc.h>
struct student
{
    int num;
    char name;
    int age;
    int sex;
    struct student *link;
}stud;
  
 creat(int n)
{
    struct student *h,*p;
    int i;
    h=(struct student *)malloc(sizeof(stud));
    if(h==NULL)
    {
        printf("not enough memory!");
        return(0);
    }
    h=NULL;
    for(i=0;i<=n;i++)
    {
         
         
        if((p=(struct student *)malloc(sizeof(stud)))==NULL)

            
            
        {
            printf("内存不足!");
            return(0);
        }
        scanf("%d",&p->num);
        scanf("%s",&p->name);
        scanf("%d",&p->age);
        scanf("%s",&p->sex);
        p->link=h;
        h=p;
    }
     void save(struct student *h)
     {
        FILE *fp;
        stud *p;
        if((fp=fopen("d:\\123","w"))==NULL)
        {
            printf("can now open file!");
            exit(1);
        }
        printf("\n saving file...\n");
        p=h;
        while(p!=NULL)
        {
            fwrite(p,sizeof(stud),1,fp);
            p=p->link;
        }
        fclose(fp);
        printf("*******save successful!********\n");
   
     
   
    }
 }
    void main()
    {
        struct student *h;
   
        h=(struct student *)creat(4);
        save(h);
    }
   
2011-09-24 18:40
embed_xuel
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:58
帖 子:3845
专家分:11385
注 册:2011-9-13
得分:0 
回复 2楼 zyx1989
好像前两天有人帖这个代码,你们都是一个老师教的吗?犯的错误都一样

总有那身价贱的人给作业贴回复完整的代码
2011-09-24 18:47
mengcan555
Rank: 4
等 级:业余侠客
帖 子:104
专家分:253
注 册:2011-9-17
得分:0 
creat(int n)
{
    struct student *h,*p;
    int i;
    h=(struct student *)malloc(sizeof(stud));
    if(h==NULL)
    {
        printf("not enough memory!");
        return(0);
    }
    h=NULL;//有致空,那上面请求内存有必要有吗?
2011-09-24 19:03
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
得分:0 
没呀,不知道。
2011-09-24 19:04
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
得分:0 
回复 4楼 mengcan555
我是写作两句了,这个没错
2011-09-24 19:06
mengcan555
Rank: 4
等 级:业余侠客
帖 子:104
专家分:253
注 册:2011-9-17
得分:0 
void main()
    {
        struct student *h;
   
        h=(struct student *)creat(4);// 你的creat函数返回值是指针吗?检查一下
        save(h);
    }
2011-09-24 19:12
zyx1989
Rank: 1
等 级:新手上路
帖 子:86
专家分:2
注 册:2011-9-17
得分:0 
回复 7楼 mengcan555
强制转换为指针变量了
2011-09-24 19:14
mengcan555
Rank: 4
等 级:业余侠客
帖 子:104
专家分:253
注 册:2011-9-17
得分:0 
  
creat(int n)
{
    struct student *h,*p;
    int i;
    h=(struct student *)malloc(sizeof(stud));
    if(h==NULL)
    {
        printf("not enough memory!");
        return(0);
    }
    h=NULL;
申请完内存再置空,有意义吗??????
2011-09-24 19:17
mengcan555
Rank: 4
等 级:业余侠客
帖 子:104
专家分:253
注 册:2011-9-17
得分:0 
我的天哪,根本就不是指针类型就能转换成结构体指针???谁教给你的,int也能强制转换成指针吗???
2011-09-24 19:19



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




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

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