调用结构体数组

Python012

调用结构体数组,第1张

主要让你了解嵌套结构体调用,我随便写一个

#include <stdio.h>

#define SIZE 5//定义5个学生

typedef struct

{

int A

int B

int C

int D

int E

}Score

typedef struct

{

int num

char name[15]

Score score

float avg

}Stu

void Average(Stu *stu)

{

int i

int sum

for (i=0i<SIZEi++)

{

sum = 0

sum += stu[i].score.A + stu[i].score.B + stu[i].score.C

+ stu[i].score.D + stu[i].score.E

stu[i].avg = (float)sum / 5

}

}

void Output(Stu *stu)

{

int i

for (i=0i<SIZEi++)

{

printf("%-10d%-15s%-5d%-5d%-5d%-5d%-5d%-5.2f\n",

stu[i].num, stu[i].name, stu[i].score.A, stu[i].score.B,

stu[i].score.C, stu[i].score.D, stu[i].score.E, stu[i].avg)

}

}

void main(void)

{

Stu stu[SIZE] =

{

{1001, "Zhangsan", {1, 2, 3, 4, 5}},

{1002, "Lisi", {1, 2, 3, 4, 5}},

{1003, "Wangwu", {1, 2, 3, 4, 5}},

{1004, "Zhaoliu", {1, 2, 3, 4, 5}},

{1005, "Baihu",{1, 2, 3, 4, 5}}

}

Average(stu)

Output(stu)

}

如果对你有所帮助,请记得采纳最佳答案,谢谢!

1、C语言结构体数组的定义:数组是有序的并且具有相同类型的数据的集合。

2、结构数组就是具有相同结构类型的变量集合。假如要用C语言,定义一个班级40个同学的姓名、性别、年龄和住址, 可以做成一个结构数组。

结构数组成员的访问是以数组元素为结构变量的,它的形式为:

1、结构数组元素—成员名。

2、例如:student[0].name,student[30].age

3、实际上结构数组相当于一个二维构造, 第一维是结构数组元素, 每个元素是一个结构变量, 第二维是结构成员。