本次课内容:结构体数组、结构体变量与函数 教学目的:掌握结构体数组的定义方法,结构体数组的 初始化和引用 重点:结构体数组的定义、函数形参及返回值为结构体 类型的引用。 难点:结构体数组元素与成员关系,函数调用的实参、 形参及返回值为结构体类型的引用。 预习:结构体类型,结构体成员及引用 sturct结构体类型名变量名表; 访问变量中的成员,用“.”将变量和成员隔开。 又如: student. birthday.day
本次课内容:结构体数组、结构体变量与函数 教学目的:掌握结构体数组的定义方法,结构体数组的 初始化和引用。 重点:结构体数组的定义、函数形参及返回值为结构体 类型的引用。 难点:结构体数组元素与成员关系,函数调用的实参、 形参及返回值为结构体类型的引用。 预习:结构体类型,结构体成员及引用 sturct 结构体类型名 变量名表; 访问变量中的成员,用“.”将变量和成员隔开。 又如:student . birthday . day
、结构体数组 个结构体变量只能存放一个对象(如一个职工 数据)的一组数据,如果存放若干职工数据,则需使用 数组,即将结构体变量定义为结构体数组。 1、定义方法: (1)先定义结构体的类型,再定义结构体数组。 如: struct stud_type i char name[10; ong huny int agey char sex; float scorer struct stud_type student[30; (2)定义结构体类型的同时定义结构体数组。 struct stud_type 3 student[30;
一、结构体数组 一个结构体变量只能存放一个对象(如一个职工 数据)的一组数据,如果存放若干职工数据,则需使用 数组,即将结构体变量定义为结构体数组。 1、定义方法: (1)先定义结构体的类型,再定义结构体数组。 如:struct stud_type { char name[10]; long num; int age; char sex; float score; }; struct stud_type student[30]; (2)定义结构体类型的同时定义结构体数组。 struct stud_type { : } student[30];
(3)直接定义结构体变量不定义类型名 如: struct Student[30; 以上方法同定义结构体变量类似,只是加方括号说明 元素个数。 2、结构体数组的初始化 如: struct stud_type i char name[10; long num int agei char sex float scorer } student3]=wang"89101,18,m:895} Zhang fun, 89102, m, 90.519 fli ling,8910320f398
(3)直接定义结构体变量不定义类型名。 如:struct { : }student[30]; 以上方法同定义结构体变量类似,只是加方括号说明 元素个数。 2、结构体数组的初始化 如:struct stud_type { char name[10]; long num; int age; char sex; float score; }student[3]={{“wang li”,89101,18,’m’,89.5}, {“zhang fun”,89102,’m’,90.5}, {“li ling”,89103,20,’f’,98}};
3、结构体数组的引用 (1)引用某元素成员 struct stud_type i char name[1oF long num: int age; char sexi float score; } student3]wang:89101,18,m,89.5} I zhang fun,89102,m, 90.5, li ling,, 89103, 20, f,, 9813 student1 printf(%ld\n", student[ 1 Inum); (2)引用数组元素 同类型元素或同类型变量间赋值 tT: student1=student[o] student[o]student[1E student studenti
3、结构体数组的引用 (1)引用某元素成员 struct stud_type { char name[10]; long num; int age; char sex; float score; } student[3]={{“wangli”,89101,18,’m’,89.5}, {“zhang fun”,89102,’m’,90.5}, {“li ling”,89103,20,’f’,98}}, student1; printf(“%ld\n”,student[ 1 ].num); (2)引用数组元素 同类型元素或同类型变量间赋值 如:student1=student[0]; student[0]=student[1]; student[1]=student1;
(3)数组元素不能整体输入输出,只能单个成员输入输出。 例# Include“ stdio include“ stdlib-h struct stud_type I char name[10] long num; int age; char sex; float score; maino i struct stud_ type student3] int if char ch, numstr[20 for (=0;i<3;i++) i printf(student%dn",i)B getsstudent[ i ]. name); gets(numstristudent[ i ]. num=atol(numstr);
(3)数组元素不能整体输入输出,只能单个成员输入输出。 例 #include “stdio.h” #include “stdlib.h” struct stud_type { char name[10] long num; int age; char sex; float score; }; main() { struct stud_type student[3]; int i; char ch,numstr[20]; for (i=0;i<3;i++) { printf(“student[%d]:\n”,i); gets(student[ i ].name); gets(numstr);student[ i ].num=atol(numstr);