结构未初始化的问题 高手指教 谢谢
#include<stdio.h>void main()
{
int i;
struct mark
{
int sub1;
int sub2;
int sub3;
int total;
};
struct mark stu[3]={{45,67,81,0},{75,53,69,0},{57,36,71,0}};
struct mark total;
for(i=0;i<3;i++)
{ stu[i].total=stu[i].sub1+stu[i].sub2+stu[i].sub3;
total.sub1=total.sub1+stu[i].sub1;
total.sub2=total.sub2+stu[i].sub2;
total.sub3=total.sub3+stu[i].sub3;
total.total=total.total+stu[i].total;
}
printf(" %d %d %d\n",total.sub1,total.sub2,total.sub3);
printf("%d\n",total.total);
}
这样输出就有问题,是初始值的问题
我改下
#include<stdio.h>
void main()
{
int i;
struct mark
{
int sub1;
int sub2;
int sub3;
int total;
};
struct mark stu[3]={{45,67,81,0},{75,53,69,0},{57,36,71,0}};
struct mark total={0,0,0,0};
for(i=0;i<3;i++)
{ stu[i].total=stu[i].sub1+stu[i].sub2+stu[i].sub3;
total.sub1=total.sub1+stu[i].sub1;
total.sub2=total.sub2+stu[i].sub2;
total.sub3=total.sub3+stu[i].sub3;
total.total=total.total+stu[i].total;
}
printf(" %d %d %d\n",total.sub1,total.sub2,total.sub3);
printf("%d\n",total.total);
}
这样就可以了 但是我不明白了 在C的结构中,不是未初始化的整数和浮点数会赋给默认值0吗,那为什么第一个程序不行?