#include<stdio.h>
struct student
{int num[10];
char name[25];
char sex;
int age;
float score[3];
}stu[
11]={{{3,1,0,6,0,0,9,4,6,9},”fangshaowu”,’M',20,{88,80,90}},{{1,0,0,0,0,0,0,0,0,0},”zhang”,’M',20,{80,88,97}},{{1,0,0,0,0,0,0,0,0,1},”Li”,’M',20,{84,79,84}},{{1,0,0,0,0,0,0,0,0,2},”Wang”,’M',20,{60,75,80}},{{1,0,0,0,0,0,0,0,0,3},”Lin”,’F',20,{88,80,94}},{{1,0,0,0,0,0,0,0,0,4},”Wong”,’F',20,{79,85,89}},{{1,0,0,0,0,0,0,0,0,5},”Shi”,’F',20,{76,80,77}},{{1,0,0,0,0,0,0,0,0,6},”Chen”,’F',20,{79,83,81}},{{1,0,0,0,0,0,0,0,0,7},”Yang”,’F',20,{90,81,90}},
{{1,0,0,0,0,0,0,0,0,8},”Liu”,’M',20,{75,84,86}}};
void main()
{
int i,j,k,l;
float sum[10],average[10],t,n;
char str[25];
printf(“\n num name sex age score1 score2 score3\n”);
for(i=0;i<10;i++)
{sum[i]=0;
for(j=0;j<10;j++)
{printf(“%d”,stu[i].num[j]);}
printf(” “);
printf(“%10s %c%5d”,stu[i].name,stu[i].sex,stu[i].age);
for(k=0;k<3;k++)
{sum[i]=stu[i].score[k]+sum[i];
printf(“%8.1f”,stu[i].score[k]);}
printf(“\n”);}
for(l=0;l<10;l++)
{for(i=0;i<10-l;i++)
if(sum[i+1]>sum[i])
{t=sum[i+1];sum[i+1]=sum[i];sum[i]=t;
stu[11]=stu[i+1];stu[i+1]=stu[i];stu[i]=stu[11];}}
for(i=0;i<10;i++)
{printf(“%8.1f”,sum[i]);}
for(i=0;i<10;i++)
{average[i]=sum[i]/3;
printf(“%8.1f”,average[i]);}
printf(“\n num name sex age score1 score2 score3\n”);
for(i=0;i<10;i++)
{for(j=0;j<10;j++)
{printf(“%d”,stu[i].num[j]);}
printf(” “);
printf(“%10s %c%5d”,stu[i].name,stu[i].sex,stu[i].age);
for(k=0;k<3;k++)
{printf(“%8.1f”,stu[i].score[k]);}
printf(“\n”);}
在里面我定义数组为stu[11],然后在下面用数组中的元素stu[11]循环,应该是没有这个元素的,但是最后却可以实现循环,谁可以帮忙跟我说下为什么。还有如果定一个函数要用到结构体数组,请问如何声明?
>> 本文固定链接: http://www.vcgood.com/archives/1584
一些比较老的编译器,对非法地址访问的检查不是特别严格.
这里虽然没有stu[11],不过根据指针地址的换算,是有这个地址和空间的,
不过访问这里是非法的,结果不可预料.
结构体数组你可以传指针,和这个结构的大小
print( stu *pstu, int num );
感谢,已理解