第十一章结构体与共用体 学习目的与要求:1.掌握结构体变量的定义与引用 2.重点掌握链表的建立、插入、删除、输出等操作。 3.了解共用体变量的定义与引用 1.掌握结构体变量的定义与引用。 2.重点掌握链表的建立、插入、删除、输出等操作。 3. switch语句。 第1讲 知识归纳 1.结构体和结构体变量的定义 格式一: tuct结构体名 ∥定义结构体 类型成员名 此是不能 struct结构体名变量名1,变量名2……;∥定义结构体变量 格式二 struct结构体名 ∥定义结构体与变量 类型成员名; }变量名1,变量名2……; (1)注意:只能对结构体变量赋值、存取或运算,而不能对结构体赋值、存取或运算 结构体是一个数据类型,与int, folat一样,都是数据类型,所以数据类型本身不能获值, 只不过结构体类型是一个构造数据类型,与数组类似 (2)一个结构体变量所占的存储空间,是各个成员所占空间的和。 2.结构体变量的引用 格式:结构体变量名成员名 )不能将一个结构体变量作为一个整体进行输入和输出,只能对结构体变量中的各 个成员分别进行输入和输出;结构体变量中的各个成员等价于普通变量 (2)一是成员运算符:他在所有运算符中优先级最高。 (3)结构体变量的成员可以进行各种运算
第十一章 结构体与共用体 学习目的与要求:1. 掌握结构体变量的定义与引用。 2. 重点掌握链表的建立、插入、删除、输出等操作。 3. 了解共用体变量的定义与引用。 重点: 1. 掌握结构体变量的定义与引用。 2. 重点掌握链表的建立、插入、删除、输出等操作。 3. switch 语句。 第 1 讲 知识归纳 1. 结构体和结构体变量的定义 格式一: struct 结构体名 //定义结构体 { 类型 成员名; } ; 此处分号不能少; struct 结构体名 变量名 1,变量名 2……;//定义结构体变量 格式二: struct 结构体名 //定义结构体与变量 { 类型 成员名; } 变量名 1,变量名 2……; (1) 注意:只能对结构体变量赋值、存取或运算,而不能对结构体赋值、存取或运算 ; 结构体是一个数据类型,与 int , folat 一样,都是数据类型,所以数据类型本身不能获值, 只不过结构体类型是一个构造数据类型,与数组类似。 (2) 一个结构体变量所占的存储空间,是各个成员所占空间的和。 2. 结构体变量的引用 格式:结构体变量名.成员名; (1) 不能将一个结构体变量作为一个整体进行输入和输出,只能对结构体变量中的各 个成员分别进行输入和输出 ;结构体变量中的各个成员等价于普通变量。 (2) .—是成员运算符;他在所有运算符中优先级最高。 (3) 结构体变量的成员可以进行各种运算
C语言程序设计上机指导与同步训练 char name [101 float scor student1 scanf( &student num, studentI name &student. age &student1 score); student age++i 3.结构体数组 (1)先定义结构体类型。 (2)然后定义结构体类型数组,方法同普通类型数组的定义。 char name [101 int age float score tudent [5] student [o]. score =480 4.指向结构体变量或数组的指针 (1)通过指向结构体变量或数组的指针,访问结构体变量或数组的成员的格式。 p)成员名 →成员名 等价于结构体变量.成员名 称为指向运算符;与成员运算符()一样,级别最高 [ int num struct strdent* p (*p).num=1001; strcpy (p-> name "Wanglin")i (*p).age=20; 基础训练(A) 填空题 1.定义结构体的关键字是
·256· C 语言程序设计上机指导与同步训练 ·256· 如: struct strdent { int num ; char name [10] ; int age ; float score; } student1 ; scanf( “%d %s%d%f” , &student1.num,student1.name , &student1.age ,&student1.score) ; student1.age++ ; 3. 结构体数组 (1) 先定义结构体类型。 (2) 然后定义结构体类型数组,方法同普通类型数组的定义。 struct strdent { int num ; char name [10] ; int age ; float score; } student [5] ; student[0].score = 480; 4. 指向结构体变量或数组的指针 (1) 通过指向结构体变量或数组的指针,访问结构体变量或数组的成员的格式。 ( * p ) . 成员名 ; p 成员名; 等价于 结构体变量. 成员名; -------称为指向运算符;与成员运算符(.) 一样,级别最高; 如: struct strdent { int num ; char name [10] ; int age ; float score; } student1 ; struct strdent* p ; p = &student1 ; (*p).num=1001; strcpy(p-> name , “Wanglin”); (*p).age=20; p -> score=480 ; 基础训练(A) 一、填空题 1. 定义结构体的关键字是____________。
第二部分¢语言程序设计同步训练 2.一个结构体变量所占用的空间是 3.有如下定义并初始化,请填写输出语句 struct person (char name [9] struct person class[4]=i"John",17 Mary, 18 nadam,16 maln ( t int i (i=0;i<4;i++) 4.指向结构体数组的指针的类型是 5.通过指针访问结构体变量成员的两种格式 、选择题 1.当说明一个结构体变量时系统分配给它的内存是() A)各成员所需内存量的总和 B)结构中第一个成员所需内存量 C)成员中占内存量最大者所需的容量 D)结构中最后一个成员所需内存量 2.在如下结构体定义中,不正确的是( A)struct teacher B)struct tea[ 201 char name[10] float sal C)struct teacher D)struct Int no: int ne char name[101 char name[ 101 float score, g tea[201 i stud[ 100] 3.根据下面的定义,能打印出May的语句是( struct person (char name[9]: int age struct person class [10]=i"John",17
第二部分 C 语言程序设计同步训练 ·257· ·257· 2. 一个结构体变量所占用的空间是____________________。 3. 有如下定义并初始化,请填写输出语句。 struct person {char name[9]; int age; } struct person class[4]={“John”,17, “Paul”,19, “Mary,18, “adam”,16 } main() { int i; for ( i=0;i<4;i++) printf(______________________________) 。 } 4. 指向结构体数组的指针的类型是___________________。 5. 通过指针访问结构体变量成员的两种格式______________和_______________。 二、选择题 1. 当说明一个结构体变量时系统分配给它的内存是( )。 A) 各成员所需内存量的总和 B) 结构中第一个成员所需内存量 C) 成员中占内存量最大者所需的容量 D) 结构中最后一个成员所需内存量 2. 在如下结构体定义中,不正确的是( )。 A)struct teacher B) struct tea[20] { { int no; Int no; char name[10]; char name[10] float salary float salary; } } C) struct teacher D) struct { { int no; int no; char name[10]; char name[10] float score; float score; } tea[20]; }stud[100]; 3. 根据下面的定义,能打印出 Mary 的语句是( )。 struct person {char name[9]; int age; } struct person class[10]={“John”,17, “Paul”,19
C语言程序设计上机指导与同步训练 Mary, 18, adam",16 A)rintf("%oc\n, class[3]. name); B)rintf( %cn,, class[1]. name[l)); C)printf("%ocin class(2 name) D)printf("%cn,, class[O] name 4.若有以下说明和语句 struct student )std,*p 则以下对结构体变量std中成员age的引用方式不正确的是( A)std age B) C)(°p).age p age 三、编程题 1.定义一个能正常反映教师情况的结构体 teacher,包含教师姓名、性别、年龄、所在部 门和薪水。 能力提高(B) 、填空题 1.以下程序用以输出结构体变量所占内存单元的字节数,请在」 内填上适当的 内容。 double x char str[50]i in() [struct per bt; printf(” bt size=dln 2.有如下定义 struct int xi char★ }tab[2]={{1,"ab"},{2,"ca"}},*p=tab 则:表达式*p>y的结果是 表达式*(++p)y的结果是 3.有如下定义
·258· C 语言程序设计上机指导与同步训练 ·258· “Mary,18, “adam”,16 } A) rintf(“%c\n”,class[3].name); B) rintf(“%c\n”,class[1].name[1]); C) printf(“%c\n”,class[2].name); D) printf(“%c\n”,class[0].name); 4. 若有以下说明和语句: struct student { int age; int num; }std, *p; p=&std; 则以下对结构体变量 std 中成员 age 的引用方式不正确的是( )。 A) std.age B) p->age C) (*p).age D) *p.age 三、编程题 1. 定义一个能正常反映教师情况的结构体 teacher,包含教师姓名、性别、年龄、所在部 门和薪水。 能力提高(B) 一、填空题 1. 以下程序用以输出结构体变量所占内存单元的字节数,请在_______内填上适当的 内容。 struct per { double x; char str[50]; }; main() {struct per bt; printf(“ bt size =%d\n”,_____________); } 2. 有如下定义 struct { int x; char *y; }tab[2]={{1,”ab”},{2,”cd”}}, *p=tab; 则:表达式*p->y 的结果是____________ 。 表达式 *(++p)->y 的结果是________________ 。 3. 有如下定义
第二部分¢语言程序设计同步训练 i int year, month, day; struct data birthdayi 对结构体变量 personl的出生年份(1980)进行赋值,请填写正确的赋值语句 4.结构数组中存有三人的姓名和年龄,以下程序输出三人中年龄最年长者的姓名和年 龄,请在 内填入正确内容 static struct man char name [20] )person[ ]=["liming",18 wanghua",19 zhangping",20 [struct int old=0 printf(w旨s旨d 、选择题 1.以下程序的运行结果是( #include <stdio. h> fint year printf("gd\n", sizeof (struct date))i 2.若有以下说明和语句,则对pup中sx域的正确引用方式是(D)1 A)6 C)10 struct pupil p=&pupi
第二部分 C 语言程序设计同步训练 ·259· ·259· struct date { int year,month,day; } struct person { char name[8]; char sex ; struct data birthday; } person1; 对结构体变量 person1 的出生年份(1980)进行赋值,请填写正确的赋值语句: __________________________________; 4. 结构数组中存有三人的姓名和年龄,以下程序输出三人中年龄最年长者的姓名和年 龄,请在____________内填入正确内容。 static struct man { char name[20]; int age; }person[ ]={“liming”,18, “wanghua”,19, “zhangping”,20 }; main() {struct man *p, *q; int old=0; p=person; for(;p______________;p++) if(old<p->age) {q=p; ________________;} printf(“%s %d”, ________________); } 二、选择题 1. 以下程序的运行结果是( )。 #include <stdio.h> main() {struct date {int year,month,day; }today; printf(“%d\n”,sizeof(struct date)); } A) 6 B) 8 C) 10 D) 12 2. 若有以下说明和语句,则对 pup 中 sex 域的正确引用方式是( )。 struct pupil {char name[20]; int sex; }pup, *p; p=&pup;