结构体调用问题。怎么给name
多重结构体调用,怎么开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;
}