| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 448 人关注过本帖
标题:多重结构体调用,怎么开name的空间啊,坐等解答。。
只看楼主 加入收藏
shuda333
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-7-30
结帖率:0
收藏
 问题点数:0 回复次数:0 
多重结构体调用,怎么开name的空间啊,坐等解答。。
x个学校 每个学校y个班级 每个班机z个学生
    计算每个学校每个班级的平均成绩及每个学校的平均成绩
    typedef struct school
    {
        sStuCla *stu_cla;
   
    }sStuSch,*pStuSch;

    typedef struct class
    {
        sStu *stu_stu;
    }sStuCla,*pStuCla;

    typedef struct student
    {
        char *name;
        int score;
    }sStu,*pStu;



我的程序刚写到开name的空间就段错误了
    #include <stdio.h>
   #include <stdlib.h>
   typedef struct student
   {
       char *name;
       int score;
   }sStu,*pStu;
   
   typedef struct class
  {
      sStu *stu_stu;
  }sStuCla,*pStuCla;
  
  typedef struct school
  {
      sStuCla *stu_cla;
  }sStuSch,*pStuSch;
  
  void my_malloc(void **p,int size)
  {
      *p=malloc(size);
      if(*p==NULL)
      {
          printf("malloc error\n");
          exit(-1);
      }
  }
     int main()
  {
      sStuSch *stu_sch;
      sStuCla *stu_cla;
      sStu *stu_stu;
      int x,y,z;
      int i,j,h;
      printf("input x,y,z:\n");
      scanf("%d%d%d",&x,&y,&z);
     int count_class[x*y],count_school[x];
      my_malloc((void **)&stu_sch,x*sizeof(sStuSch));
      my_malloc((void **)&stu_cla,x*y*sizeof(sStuCla));
      my_malloc((void **)&stu_stu,x*y*z*sizeof(sStu));
      printf("%p\t%p\t%p\n",stu_sch,stu_cla,stu_stu);
      for(i=0;i<x;i++)
      {
          for(j=0;j<y;j++)
          {
              for(h=0;h<z;h++)
              {
                  printf("input student name,score\n");
                  printf("+++++++++++++++++++++++++++\n");
                  my_malloc((void **)&(stu_sch[i].stu_cla[j].stu_stu[h].name),20);
                  printf("------------------------\n");
                  scanf("%s%d",stu_sch[i].stu_cla[j].stu_stu[h].name,
                          &stu_sch[i].stu_cla[j].stu_stu[h].score);
              }
          }
      }
  
      for(i=0;i<x;i++)
      {
          for(j=0;j<y;j++)
          {
             for(h=0;h<z;h++)
              {
             printf("%s\t%d\n",stu_sch[i].stu_cla[j].stu_stu[h].name,
            stu_sch[i].stu_cla[j].stu_stu[h].score);
             }
  
         }
      }
      return 0;
  }


[ 本帖最后由 shuda333 于 2013-8-10 10:45 编辑 ]
搜索更多相关主题的帖子: include school 结构体 空间 
2013-08-10 10:33
快速回复:多重结构体调用,怎么开name的空间啊,坐等解答。。
数据加载中...
 
   



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

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