李晗静双语C语言教学课件双语[6-structure]

上传人:清晨86****784 文档编号:302891450 上传时间:2022-06-02 格式:PPT 页数:72 大小:308KB
返回 下载 相关 举报
李晗静双语C语言教学课件双语[6-structure]_第1页
第1页 / 共72页
李晗静双语C语言教学课件双语[6-structure]_第2页
第2页 / 共72页
李晗静双语C语言教学课件双语[6-structure]_第3页
第3页 / 共72页
李晗静双语C语言教学课件双语[6-structure]_第4页
第4页 / 共72页
李晗静双语C语言教学课件双语[6-structure]_第5页
第5页 / 共72页
点击查看更多>>
资源描述

《李晗静双语C语言教学课件双语[6-structure]》由会员分享,可在线阅读,更多相关《李晗静双语C语言教学课件双语[6-structure](72页珍藏版)》请在金锄头文库上搜索。

1、Chapter 6 Structures structures 结构体 typedef unions 共用体 bit-fields 6.1 Basics of structuresThe definition of structure types 类型定义The definition of structure variables 变量定义The reference of structure variables 变量引用The initialization of structure variables 变量初始化Notes6.1.1 The definition of structure typ

2、es The definition of structure types1. struct point 3. struct student int x; long num; int y; char name10; ; char sex; 2. struct date int age; int year; float score; int month; ; int day;6.1.1 The definition of structure typesstruct 结构体名 类型标识符 成员名; 类型标识符 成员名; .;成员类型可以是基本型或构造型struct是关键字,不能省略合法标识符可省:无

3、名结构体例 struct student int num; char name20; char sex; int age; float score; char addr30; ; namenumsexagescoreaddr2字节2字节20字节1字节4字节30字节.结构体类型定义描述结构的组织形式,不分配内存结构体类型定义的作用域6.1.2 The definition of structure variables The definition of structure variables 2. struct point int x; int y; pt;1. struct point pt;

4、 struct student s; struct date d;6.1.2 The definition of structure variables3. struct int year; int month; int day; d;4.#define STUDENT struct student STUDENT long num; char name10; char sex; int age; float score; ;STUDENT s;Nesting例 struct student int num; char name20; struct date int month; int da

5、y; int year; birthday; stu;numnamebirthdaymonthdayyear例 struct date int month; int day; int year; ; struct student int num; char name20; struct date birthday; stu;num namebirthdaymonth day year6.1.3 The reference of structure variables The reference of structure variables struct point pt; struct dat

6、e d; pt.x=3; pt.y=5; scanf(“%d%d%d”,&d.year,&d.month,&d.day); printf(“%d,%dn”,pt.x,pt.y); printf(“%d,%d,%dn”, d.year,d.month,d.day);6.1.3 The reference of structure variablesThe reference of structure variables struct student s; s.num=97601; strcpy(name, “wang”); s.sex=m; s.age=20; s.score=94.5; pri

7、ntf(“%ld,%s,%c,%d,%fn”,s.num,s.name,s.sex,s.age,s.score);结构体变量的引用引用规则 结构体变量不能整体引用,只能引用变量成员可以将一个结构体变量赋值给另一个结构体变量结构体嵌套时逐级引用成员(分量)运算符优先级: 1结合性:从左向右引用方式: 结构体变量名.成员名例 struct student int num; char name20; char sex; int age; float score; char addr30; stu1,stu2; stu1.num=10;stu1.score=85.5;stu1.score+=stu2.

8、score; stu1.age+;例 struct student int num; char name20; char sex; int age; float score; char addr30; stu1,stu2; printf(“%d,%s,%c,%d,%f,%sn”,stu1); ()stu1=101,“Wan Lin”,M,19,87.5,“DaLian”; ()例 struct student int num; char name20; char sex; int age; float score; char addr30; stu1,stu2; stu2=stu1; ( )例

9、 struct student int num; char name20; struct date int month; int day; int year; birthday; stu1,stu2;numnamebirthdaymonthdayyearstu1.birthday.month=12;例 struct student int num; char name20; char sex; int age; float score; char addr30; stu1,stu2; if(stu1=stu2). ()6.1.4 The initialization of structure

10、variables 1: struct student int num; char name10; char sex; int age; float score; s=97601,”wang”,m,20,98.5;22:struct student int num; char name20; char sex; int age; char addr30; ; struct student stu1=112,“Wang Lin”,M,19, “200 Beijing Road”;3: struct int num; char name20; char sex; int age; char add

11、r30; stu1=112,“Wang Lin”,M,19, “200 Beijing Road”; 6.1 Basics of structuresNotes 1.assign,access and operate only act on the variables,not the types. When the program is compiled, the variables are allocated storage spaces,not the type. 编译时分配变编译时分配变量存储空间量存储空间2.The member of a structure variable may

12、be another structure variable. 可以嵌套可以嵌套6.1 Basics of structures structure student int num; char name10; char sex; s.age.year=1979; struct date age; s.age.month=10; float score; s.age.day=1; s; Structures can be nested.A rectangle is a pair of points. struct rect struct point pt1; struct point pt2; ;

13、6.1 Basics of structures6.1 Basics of structuresWe can declare variable screen: struct rect screen; screen.pt1.xrefers to the x coordinate of the pt1 member of screen. 3.A structure member or tag and an ordinary variable can have the same name without conflict. 成员和变量可以重名4.The ANSI standard is to def

14、ine structure assignment-copy,assign,passed to functions, returned by function. 5.A structure is same as an array.It is a collection of one or more elements. But the elements of an array are all the same type,for a structure may be different types. 成员可以不同类型6.1 Basics of structures6.2 Structures and

15、Functions 结构体和函数Return a value which is a structure type 返回值类型 struct point makepoint(int x,int y) struct point temp; temp.x=x; temp.y=y; return temp; The argument name and the member with the same name , but there is no conflict.do arithmetic on points 参数是结构体类型 /*addpoint:add two points*/ struct po

16、int addpoint (struct point p1,struct point p2) p1.x+=p2.x; p1.y+=p2.y; return p1;Here both the arguments and the return value are structures.6.2 Structures and FunctionsTests whether a point is inside a rectangle.检查一个点是否在矩形之中 /*ptinrec:return 1 if p in r,0 if not*/ int ptinrec(struct point p,struct rect r) return p.x = r.pt1.x & p.x = r.pt1.y & p.yx+pp-x increments x, not pp(+pp)-x increments pp before accessing x(pp+)-x increments pp afterward The structure operators . and -,together with ( ) f

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号