《C语言英文Chapter 10(课堂PPT)》由会员分享,可在线阅读,更多相关《C语言英文Chapter 10(课堂PPT)(51页珍藏版)》请在金锄头文库上搜索。
1、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 initialization 10.3 structure array 10.4 structure pointer 10
2、.5 link dispoing- application of structure pointer 10.6 union and enumeration 10.7 typedef,2,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 emerging STUDENT SCORES MANAGE TABLE CANOT
3、DEAL WITH 2D ARRAYS,DIFFERENT TYEPES!,3,二、 structure type declaration Common 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,4,demonstration: 1、the
4、rules to name“structure type name”and “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
5、 been declarated. eg.the type”birthday” in the eg. Of structure type std_info is a definited structure type -date,5,三、 STRUCTURE VARIABLES DECLARATION Structure type(user definition) Standart type(system definition) 1、indirect declaration first declare the structure type,then the structure variables
6、,The same usage,struct student int num; char name20; char sex; int age; float score1; float score2; ;,struct student stu1, stu2;,num name sex age score1 score2,stu1,6,2、 direct declaration (1)definite the structure variable,when definiting the structure type; (2)definite the structure variable direc
7、tly;,(1) struct student int num; char name20; float score2; stu1, stu2;,(2) 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 b
8、e same as other variables in program, stand for different objects,7,eg10.1 creat a structure reffering to basic information of a student to store the details of a student struct date /*data structure type made of year,month and day*/ int year; int month; int day; ; struct std_info/*student informati
9、on structure type*/ int num; /*made of name,sex,birthday,etc*/ char name12; char sex; struct date birthday; float score1,score2; ;,8,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 th
10、e student details #include“struct.h”struct std_info stu = 1002,”Zhang San”,M, 1980,9,20,98.2,86.5;main() printf(No: %dn,stu.num); printf(Name: %sn,stu.name); printf(Sex: %cn,stu.sex); printf(Birthday: %d-%d-%dn,stu.birthday.year, stu.birthday.month, stu.birthday.day); ,9,result: No: 1002 Name: Zhang
11、 San Sex: M Birthday:1980-9-20,10,1、 INITIALIZATIONS OF STUCTURE variable structtype struct_variable=initial value list eg,struct std_info stu = “000102”,”Zhang San”,M, 1980,9,20; note:data type of initial value should be consistent with the members in structure variable,or there will be error. 2、ci
12、ting 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。,11,NOTE:if a member itself is a structure type,a multilevel member operation is needed to cite the lowest member. Expanding fo
13、rmat: structure variable. member.child-member. Lowest member Eg.: stu.birthday.year stu.birthday.month stu.birthday.day betaking the lowest member is equal to common variable of the same type.,12,10.3 STRUCTURE ARRAYS,Structure arrays:an array made by structure type data which is as element in struc
14、ture. Every array element:include all members of structure 例10.3 use structure array to store and print the student scores manage table below.,stu0 stu1 stu2 stu3,13,struct student /*lay the definition of structure type outside function*/ int num; char name12; char sex; int age; float score1; float
15、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”,14,#includestruct.h” main() int i; /*print list head, “stands for one backspace*/ printf(No.NameSexAgesoc1 sco2n); /*output 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); ,15,Running result: No. Name Sex Age soc1 sco2