C语言英文课件Chapter.ppt
《C语言英文课件Chapter.ppt》由会员分享,可在线阅读,更多相关《C语言英文课件Chapter.ppt(51页珍藏版)》请在三一办公上搜索。
1、chapter 10 STRUCTURES AND UNIONS,A structure is a collection of one or more variables,possibly of different types,which are treated as a unit instead of as separate entities.10.1 structure definition 10.2 structure variables citing and initialization10.3 structure array10.4 structure pointer10.5 lin
2、k dispoing-application of structure pointer10.6 union and enumeration10.7 typedef,10.1 STRUCTURE TYPE AND DECLARATION OF STRUCTURE VARIABLES,THE STRUCTURE TYPE,IN C LANGUAGE,is the counterpart as“record”in other advanced language.一、question emergingSTUDENT SCORES MANAGE TABLE CANOT DEAL WITH 2D ARRA
3、YS,DIFFERENT TYEPES!,二、structure type declarationCommon usage:struct structure typename datatype member1;datatype member2;datatype membern;,struct student int num;char name20;char sex;int age;float score1;float score2;,Type name,struct student,demonstration:1、the rules to name“structure type name”an
4、d“member name”,are same with the variables.2、the definitions of variables can be seperated or in a same line like the instance below eg.struct date int year,month,day;3、the definitions of variables could be basic types or another structure type which has been declarated.eg.the type”birthday”in the e
5、g.Of structure type std_info is a definited structure type-date,三、STRUCTURE VARIABLES DECLARATIONStructure type(user definition)Standart type(system definition)1、indirect declaration first declare the structure type,then the structure variables,The same usage,struct student int num;char name20;char
6、sex;int age;float score1;float score2;,struct student stu1,stu2;,num name sex age score1 score2,stu1,2、direct declaration(1)definite the structure variable,when definiting the structure type;(2)definite the structure variable directly;,(1)struct student int num;char name20;float score2;stu1,stu2;,(2
7、)struct int num;char name20;float score2;stu1,stu2;,3、demonstration(1)differ structure type and variable,which are completely different conception,just as type int and variable of int(2)member names in structure,may be same as other variables in program,stand for different objects,eg10.1 creat a str
8、ucture reffering to basic information of a student to store the details of a studentstruct date/*data structure type made of year,month and day*/int year;int month;int day;struct std_info/*student information structure type*/int num;/*made of name,sex,birthday,etc*/char name12;char sex;struct date b
9、irthday;float score1,score2;,10.2 CITING AND INITIALIZATIONS OF STUCTURE VARIABLES,例10.2 make use of the former stucture type struct std_info,to definite a structure type stu to store and print the student details#include“struct.h”struct std_info stu=1002,”Zhang San”,M,1980,9,20,98.2,86.5;main()prin
10、tf(No:%dn,stu.num);printf(Name:%sn,stu.name);printf(Sex:%cn,stu.sex);printf(Birthday:%d-%d-%dn,);,result:No:1002Name:Zhang SanSex:MBirthday:1980-9-20,1、INITIALIZATIONS OF STUCTURE variable structtype struct_variable=initial value listeg,struct std_info stu=“000102”,”Zhang San”,M,1980,9,20;note:data
11、type of initial value should be consistent with the members in structure variable,or there will be error.2、citing rules of structure variable member operator“.”while citing the member in structure,use operator”.”;citing format:structure variable.member eg,stu.num;stu.name,etc。,NOTE:if a member itsel
12、f is a structure type,a multilevel member operation is needed to cite the lowest member.Expanding format:structure variable.member.child-member.Lowest memberEg.:betaking the lowest member is equal to common variable of the same type.,10.3 STRUCTURE ARRAYS,Structure arrays:an array made by structure
13、type data which is as element in structure.Every array element:include all members of structure例10.3 use structure array to store and print the student scores manage table below.,stu0stu1stu2stu3,struct student/*lay the definition of structure type outside function*/int num;char name12;char sex;int
14、age;float score1;float score2;struct stdent stu4=1001,”Zhang Xin”,M,20,87,91,1002,”Wang Li”,F,20,98,96,1003,”Chen Fong”,M,21,86,90,1004,”Li Xiaopen”,M,20,77,86;,Store in file“struct.h”,#includestruct.h”main()int i;/*print list head,“stands for one backspace*/printf(No.NameSexAgesoc1 sco2n);/*output
15、basic details of 4 students*/for(i=0;i4;i+)printf(%-5d,stui.num);printf(%-12s,stui.name);printf(%-4c,stui.sex);printf(%-4d,stui.age);printf(%-5.1f,stui.score1);printf(%-5.1fn,stui.score2);,Running result:No.Name Sex Age soc1 sco2 1001 Zhang Xin M 20 87 91 1002 Wang Li F 20 98 96 1003 Chen Fong M 21
16、86 90 1004 Li Xiaopen M 20 77 86,eg10.4use arrays of structure as funtion parameters#includestruct.hvoid print_stulist(struct student stu)int i;printf(No.Name Sex Age soc1 sco2n);for(i=0;i4;i+)printf(%-5d,stui.num);printf(%-12s,stui.name);printf(%-4c,stui.sex);printf(%-4d,stui.age);printf(%-5.1f,stu
17、i.score1);printf(%-5.1fn,stui.score2);,Use Array of structure as formal parameter,void sort(struct student st)int i,j;struct student temp;for(i=0;i4-1;i+)for(j=i+1;j4;j+)if(sti.score1stj.score1)temp=sti;sti=stj;stj=temp;,main()clrscr();print_stulist(stu);sort(stu);printf(list after sorted:n);print_s
18、tulist1(stu);getch();,Structure array as formal parameter,Running result:No.Name Sex Age soc1 sco2 1001 Zhang Xin M 20 87 91 1002 Wang Li F 20 98 96 1003 Chen Fong M 21 86 90 1004 Li Xiaopen M 20 77 86 list after sorted:No.Name Sex Age soc1 sco2 1002 Wang Li F 20 98 96 1001 Zhang Xin M 20 87 91 1003
19、 Chen Fong M 21 86 90 1004 Li Xiaopen M 20 77 86,10.4 pointer pointed to data type of structure,Pointer of structure variable:structure variable exists in the starting position of the storage.void print_stulist(struct student stu)int i;printf(No.Name Sex Age soc1 sco2n);for(i=0;i4;i+)printf(%-5d,stu
20、i.num);printf(%-12s,stui.name);printf(%-4c,stui.sex);printf(%-4d,stui.age);printf(%-5.1f,stui.score1);printf(%-5.1fn,stui.score2);,modify!Use pointer which point to structure as formal parameter,eg10.5 use pointer to call member of the structure variablevoid print_stulist1(struct student*p)int i;pri
21、ntf(No.Name Sex Age soc1 sco2n);for(i=0;i4;i+)printf(%-5d,(*(p+i).num);printf(%-12s,(*(p+i).name);printf(%-4c,(*(p+i).sex);printf(%-4d,(*(p+i).age);printf(%-5.1f,(*(p+i).score1);printf(%-5.1fn,(*(p+i).score2);,If usefor(i=0;i4;i+,p+),then how to modify?,eg10.5 use pointer to call every member of the
22、 structure variablevoid print_stulist1(struct student*p)int i;printf(No.Name Sex Age soc1 sco2n);for(i=0;i4;i+,p+)printf(%-5d,(*p).num);printf(%-12s,(*p).name);printf(%-4c,(*p).sex);printf(%-4d,(*p).age);printf(%-5.1f,(*p).score1);printf(%-5.1fn,(*p).score2);,If the pointer has been pointing to stru
23、cture variable v,then when:struct student var,*p=The 3 forms below are equal:(1)var.member name(2)(*p).member name(3)p-member namethoughts:how to input every member of stui by keyboard?,10.5 linklist disposingapplication of structure pointer,10.5.1 linklist overview 一Dynamic data structure element n
24、umber fluctuates by program running.cf.data structure of static state(eg.array),confirm ing its size when definiting.二 Dynamic data structure implement in virtue of:1、pointer points to structure type 2、structure type including pointer data linklist:a simple dynamic data structure.head,10.5.2 creatin
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 英文 课件 Chapter

链接地址:https://www.31ppt.com/p-5426521.html