清华大学出版社 TSINGHUA UNIVERSITY PRESS 9.1.3引用结构体变量 但不能用以下语句整体读入结构体变量, 例如: scanf("%d %s %c %d %f %s &student1) 结构体变量的地址主要用作函数参数, 传递结构体变量的地址
但不能用以下语句整体读入结构体变量, 例如: scanf(″%d,%s,%c,%d,%f,%s″, &student1); 结构体变量的地址主要用作函数参数, 传递结构体变量的地址。 9.1.3 引用结构体变量
清华大学出版社 TSINGHUA UNIVERSITY PRESS 9.1.4结构体变量的初始化 例9,1对结构体变量初始化 #include <stdio. h> 运行结果 void main O Istruct student No.:10101 (long int num name: Lilin char name [20]: sex: M char sex: address: 123 Beijing road char addr[20] }a={10101," LiLin″,′M′ 123 Beijing Road"};对结构体变量a赋初值 printf(" No: %1d nname: %s\ sex: %c\ address: %s \n", a num, a name, a sex, a addr)
9.1.4 结构体变量的初始化 但不能用以下语句整体读入结构体变量, 例如: scanf(″%d,%s,%c,%d,%f,%s″, &student1); 结构体变量的地址主要用作函数参数, 传递结构体变量的地址。 例9.1 对结构体变量初始化. #include <stdio.h> void main() {struct student {long int num; char name[20]; char sex; char addr[20]; }a={10101,″LiLin″,′M′,″123 Beijing Road″}; /* 对结构体变量a赋初值*/ printf(″No.:%ld\nname:%s\nsex:%c\naddress:%s \n″,a.num,a.name,a.sex,a.addr); } 运行结果: No.:10101 name:LiLin sex:M address:123 Beijing Road
清华大学出版社 TSINGHUA UNIVERSITY PRESS §9.2结构体数组 一个结构体变量中可以存放一组数 据(如一个学生的学号、姓名、成绩等 数据)。如果有10个学生的数据需要 参加运算,显然应该用数组,这就是结 构体数组。结构体数组与以前介绍过的 数值型数组不同之处在于每个数组元素 都是一个结构体类型的数据,它们都分 别包括各个成员(分量)项
§9.2 结构体数组 一个结构体变量中可以存放一组数 据(如一个学生的学号、姓名、成绩等 数据)。如果有10个学生的数据需要 参加运算,显然应该用数组,这就是结 构体数组。结构体数组与以前介绍过的 数值型数组不同之处在于每个数组元素 都是一个结构体类型的数据,它们都分 别包括各个成员(分量)项
清华大学出版社 TSINGHUA UNIVERSITY PRESS §9.2结构体数组 9.2.1定义结构体数组 和定义结构体变量的方法相仿,只需说明 其为数组即可。例如 struct student (int num; char name[20]; char sex; int age float score: char addr[以上定义了一个数 F; struct student[3]: 组stu,数组有3个元 素,均为 struct student类型数据
§9.2 结构体数组 9.2.1 定义结构体数组 和定义结构体变量的方法相仿,只需说明 其为数组即可。例如: struct student {int num;char name[20];char sex;int age; float score;char addr[30]; };struct student[3]; 以上定义了一个数 组stu,数组有3个元 素,均为struct student类型数据
清华大学出版社 TSINGHUA UNIVERSITY PRESS §9.2结构体数组 也可以直接定义一个结构体数组,例如 struct student fint num: num name sex age score addr stu[o] 10101 Li Lin M 18 87.5 103 Beijing Road stu[1] 10102 Zhang Fun M 1999130 Shanghai Road stu[2] 10104 Wang Min F20785 1010 Zhongshan Road 图9-4
§9.2 结构体数组 也可以直接定义一个结构体数组,例如: struct student {int num; …};stu[3]; 或: strcut student {int num; …};stu[3]; 图9-4