
第29次课结构体
第29次课 结 构 体

学号姓名成绩张三941001李四100264王五861003陈六741004李七921005学号姓名性别出生日期年月日2张三M110012001W李四213100220005M12王五100319997陈六M810042000W李七52210052000
学号 姓名 成绩 1001 张三 94 1002 李四 64 1003 王五 86 1004 陈六 74 1005 李七 92 学号 姓名 性别 出生日期 年 月 日 1001 张三 M 2001 1 2 1002 李四 W 2000 2 13 1003 王五 M 1999 12 5 1004 陈六 M 2000 8 7 1005 李七 W 2000 5 22

结构体类型(定义)结构体是一组相关变量(或数组)的集合,而且这组变量(或数组)的类型可以互不相同。构成结构体的变量称为结构体的成员。一般地,定义一个有n个成员的结构体类型可以采用如下形式:【格式】结构体类型符struct1类型标识符1成员名1;类型标识符2成员名2;类型标识符n成员名n;1 ;
结构体是一组相关变量(或数组)的集合,而且 这组变量(或数组)的类型可以互不相同。构成结构 体的变量称为结构体的成员。 一般地,定义一个有n个成员的结构体类型可以采 用如下形式: 【格式】 struct 结构体类型符 { 类型标识符1 成员名1; 类型标识符2 成员名2; . . 类型标识符n 成员名n; } ; 1 结构体类型(定义)

(定义)结构体类型例:学号姓名成绩datestruct1 int year;例:int month;studstructnt day;< intnum;7;char name[10];studentstructfloat score;<intnum;3;charname[10];charsex;学号性别姓名出生日期struct datebirthday:年月日char addr[20];7;
例: struct date { int year; int month; nt day; }; struct student { int num; char name[10]; char sex; struct date birthday; char addr[20]; }; 例: struct stud { int num; char name[10]; float score; }; 1 结构体类型 (定义)

2结构体类型变量定义结构体类型定义后,该标识符的使用就如其它类型标识符(如int、float、char等)一样的使用,即可以定义该类型变量变量名列表struct结构体类型符例如:studstructintnum;charname[10] ;floatscore; ;studa,b;struct
结构体类型定义后,该标识符的使用就如其它类型标识符 (如int、float、char等)一样的使用,即可以定义该类型变量。 struct 结构体类型符 变量名列表; 例如: struct stud { int num; char name[10]; float score; }; struct stud a,b; 2 结构体类型 变量定义

2变量定义结构体类型结构体变量还可用以下方法定义:直接定义结构体变量:定义结构体名同时定义变量:structstudstruct(intnum;intnum;charname[10] ;charname[10] ;floatscore;floatscore;Ia, b;Ia, b;使用typedef定义类型别名利用typedef为结构体类型定义别名,可简化代码,提高可读性例如:STU;typedefstruct studSTU a,b; 等价于: struct stud a,b;
➢直接定义结构体变量: struct { int num; char name[10]; float score; }a,b; 结构体变量还可用以下方法定义: ➢定义结构体名同时定义变量: struct stud { int num; char name[10]; float score; }a,b; 2 结构体类型 变量定义 ➢使用typedef定义类型别名 利用typedef为结构体类型定义别名,可简化代码,提高可读性 例如: typedef struct stud STU; STU a,b; 等价于: struct stud a,b;

变量引用结构体类型除对结构体变量赋值或作为参数传递可直接对个结构体变量整体操作外,其它情况下只能对结构体变量的各每个成员分别引用,其引用形式为:【格式】结构体变量名:成员名其中“"叫做成员运算符,它在所有的运算符中优先级最高。例如 a.numa.namea.score
除对结构体变量赋值或作为参数传递可直接对一 个结构体变量整体操作外,其它情况下只能对结构体变 量的各每个成员分别引用,其引用形式为: 【格式】 结构体变量名. 成员名 其中“.”叫做成员运算符,它在所有的运算符中 优先级最高。 例如:a.num a.name a.score 3 结构体类型 变量引用

结构体类型变量引用可对结构体类型变量的成员进行同类型变量一样的各种运算。sum=a.score+b.score;a.num=1001;strcpy(a.name,"张三");a.score++;等均为合法语句
可对结构体类型变量的成员进行同类型变量一样 的各种运算。 sum=a.score+b.score; a.num=1001; strcpy(a.name, "张三"); a.score++; 等均为合法语句。 3 结构体类型 变量引用

结构体类型变量引用#include int main(1studstruct[ intnum;charname[10] ;floatscore;Ta, b;scanf("%d %s %f", &a. num, a.name, &a. score) ;b. num=11;strcpy(b.name,"李四");b. score=89. 5;printf("%d, %s,%fln",a. num, a. name,a. score) ;printf("%d, %s, %f\n", b. num, b. name, b. score) ;return O;
#include int main() { struct stud { int num; char name[10]; float score; }a,b; scanf("%d %s %f",&a.num,a.name,&a.score); b.num=11; strcpy(b.name,"李四"); b.score=89.5; printf("%d,%s,%f\n",a.num,a.name,a.score); printf("%d,%s,%f\n",b.num,b.name,b.score); return 0; } 3 结构体类型 变量引用

结构体类型变量引用在某些情况下可以对结构体变量的整体操作。例:structstu intnum;char name[20];float scoreI a,b;b. num=11;strcpy(b.name,"李四");b. score=89. 5;a=b;
在某些情况下可以对结构体变量的整体操作。 例: struct stu { int num; char name[20]; float score } a,b; b.num=11; strcpy(b.name,"李四"); b.score=89.5; a=b; 3 结构体类型 变量引用