清华大学出版社 TSINGHUA UNIVERSITY PRESS §11.3结构体变量的引用 (4)可以引用结构体变量成员的地址,也可以引 用结构体变量的地址。 例如 scanf("‰d″,& student1num) (输入 student1num的值) printf("%o",&student1) (输出 student1的首地址)
§11.3结构体变量的引用 (4) 可以引用结构体变量成员的地址,也可以引 用结构体变量的地址。 例如: • scanf(″%d″,&student1.num); (输入student1.num的值) • printf(″%o″,&student1); (输出student1的首地址)
清华大学出版社 TSINGHUA UNIVERSITY PRESS §11.3结构体变量的引用 但不能用以下语句整体读入结构体变量, 例如: scanf("%d %s %c %d %f %s &student1) 结构体变量的地址主要用作函数参数, 传递结构体变量的地址
§11.3结构体变量的引用 但不能用以下语句整体读入结构体变量, 例如: scanf(″%d,%s,%c,%d,%f,%s″, &student1); 结构体变量的地址主要用作函数参数, 传递结构体变量的地址
清华大学出版社 TSINGHUA UNIVERSITY PRESS 811.4结构体变量运行结果 例11.1对结构体变量初 No.10101 #include <stdio. h> name: LiLin void main O) seX. M Istruct student address: 123 Beijing Road (long int num char name [20] char sex: 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)
§11.4结构体变量的初始化 但不能用以下语句整体读入结构体变量, 例如: scanf(″%d,%s,%c,%d,%f,%s″, &student1); 结构体变量的地址主要用作函数参数, 传递结构体变量的地址。 例11.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 §11.5结构体数组 一个结构体变量中可以存放一组数 据(如一个学生的学号、姓名、成绩等 数据)。如果有10个学生的数据需要 参加运算,显然应该用数组,这就是结 构体数组。结构体数组与以前介绍过的 数值型数组不同之处在于每个数组元素 都是一个结构体类型的数据,它们都分 别包括各个成员(分量)项
§11.5 结构体数组 一个结构体变量中可以存放一组数 据(如一个学生的学号、姓名、成绩等 数据)。如果有10个学生的数据需要 参加运算,显然应该用数组,这就是结 构体数组。结构体数组与以前介绍过的 数值型数组不同之处在于每个数组元素 都是一个结构体类型的数据,它们都分 别包括各个成员(分量)项
清华大学出版社 TSINGHUA UNIVERSITY PRESS §11.5结构体数组 1151定义结构体数组 和定义结构体变量的方法相仿,只需说明 其为数组即可。例如 struct student fint num; char name[20]; char sex; int age float score; char add以上定义了一个数 l' struct student[3]. 素,均为stur 组stu,数组有3个 student类型数据
§11.5 结构体数组 11.5.1定义结构体数组 和定义结构体变量的方法相仿,只需说明 其为数组即可。例如: struct student {int num;char name[20];char sex;int age; float score;char addr[30]; };struct student[3]; 以上定义了一个数 组stu,数组有3个元 素,均为struct student类型数据