1、Structure Xue QingXue Qing2009.42009.4Structure What is StructureDefinitionInitializationAccess the members of structureStructure and pointerStructure and function Structure and arraynumber, name , score1, score2, score3, score4 What is StructureIn order to deal with the special data which has sever
2、al member with different data type, it is need to introduce new data type. Array an ordered set with the same data type.1090569731int a823654081int a24int no , char name20 , float s1, s2, s3, s4one student: strutrueWhat is StructureStructure1 Structure is a advanced data type in C.2 Structure is a m
3、ethod for grouping a several related data type together.3 Variables with different types can be grouped in a structureThe simple variables : int a , b; a=20, b=85; - invidual same type relatedgrade0grade1grade2grade3What is Structurenumnamegrade1grade210Li90.5A11Liu80B12Wen88B . intcharfloatchar How
4、 can we define related data that have different type ? struct int num; char name10; float grade1; char grade2; ; What is Structure All variables with the same types can be grouped in a array.wang98889887li87668367zhan78877561liu90768172zhao87817471 All variables with different types can be grouped i
5、n a structureContent nameagesexaddwang18m3-110li20f7-121liu17f7-212zhao18m3-222structure members ( type ,name)Structure variables Definition1 32 A structure is a collection of related variables, any number of type of variables may be included within it.Defining structure,then declaring struct variab
6、les.Defining structure and struct variables.structure s name can be omitted keyword struct Structure members Sturcture variables Structure variable valuesstruct name type1 member 1; type2 member 2; typen member n; ;1 Defining structure,then declaring struct variables.Definition struct student int nu
7、m; char name10; float grade1; char grade2; ; Structure is only as structure template, not occupying memory, because no any variable yet. A) Defining structure numname grade1grade2struct data char name20; int age; char address30; long telephone; struct struct_name struct_variable_name;struct data wan
8、g; struct data li,zhang; DefinitionB) Declaring structure variable nameageaddresstelephonename20 age20 bytes2 bytes30 bytes4 bytesaddresstelephonename20ageaddress30telephonestruct data char name20; int age; char address30; long telephone; ; struct data wang;After declaration, computer will assign me
9、mory units to them. Definition main ( ) char str20; struct date int year, month, day; today; struct address char name30, street40, city20, state2; unsigned long int zip; wang; printf(char: %dt, sizeof(char); printf(“int: %dt”, sizeof(int); printf(long: %dt, sizeof(long); printf(float: %dn, sizeof(fl
10、oat); printf(double: %dt, sizeof(double); printf(str: %dt, sizeof(str); printf(date: %dt, sizeof(struct date); printf(“wang: %dn”, sizeof (wang ) ); Definition2 . Defining structure and struct variables. struct name type1 member 1; type2 member 2; typen membern; variable 1, variable2.; struct studen
11、t int num; char name20; char sex; int age; float score; char addr30; stu1,stu2; numnamesexagescoreaddstu1stu2Definition3 .structure s name can be omitted struct type1 member 1; type2 member 2; typen membern; variable 1, variable2.; struct int num; char name20; char sex; int age; float score; char ad
12、dr30; stu1,stu2; Definitionnumnamesexagescoreaddr201wangM2095Zhongguancun road202liF1980Guangda GardenInitialization struct student int num; char name20; char sex; int age; char addr30; ; struct student stu1= 112,“Wang Lin”,M,19, “200 Beijing Road”; When defining structure ,assign values to each str
13、uct_variable (each member) one by one . Should be agreed with the type of corresponding members. struct student int num; char name20; char sex; int age; char addr30; stu1= 112,“Wang Lin” , M, 19, “200 Beijing Road” ; struct int num; char name20; char sex; int age; char addr30; stu1= 112,“Wang Lin”,M
14、,19, “200 Beijing Road” ; InitializationHow to use these values? Access structure membersstud1 . grade1=95;stud1 . grade2=A;where “ . ” is called struct_ var_name . member_name struct int num; char name10; float grade1; char grade2 ; stud1,stud2,stud3; scanf ( “ %s , %f , %c, ”, &stud2.name, &stud2.
15、grade1, &stud2.grade2);printf ( “ %s , %f , %c , ”, stude2.name, stude2 grade1, stude2 grade2);/* assignment */main( ) struct char initial; /* last name initial */ int age; /* childs age */ int grade; /* childs grade in school */ boy , girl; boy.initial = R; boy.age = 17; boy.grade = 75; girl.age =
16、boy.age - 1; /* she is one year younger */ girl.grade = 82; girl.initial = H; printf(%c is %d years old and got a grade of %dn, girl.initial, girl.age, girl.grade); printf(%c is %d years old and got a grade of %dn, boy.initial, boy.age, boy.grade);Access structure membersOne structure may be nested
17、within another.namework addressHome addresspostaddr telpostaddrtelstruct address int post char addr100; char tel20; ;struct personal char name20; struct address workaddr ; struct address homeaddr ; ;struct_var_name. out_mem . inner_memmain ( ) struct date /* structure type date */ int year, month, d
18、ay; ; struct date today; /* stru-var today */ printf (Enter today date:); scanf(“%d %d %d”, &today.year, &today.month, &today.day ) ; printf(“%d.%d.%dn”, today.year,today.month,today.day ); datedate& &today.daytoday.daytoday.daytoday.dayAccess structure members struct student int no char name20; cha
19、r sex; struct int year; int month; int day; birth; ;struct student s;A)year=1982 month=11 day=11B)birth.year=1982, birth.month=11 birth.day=11C)s.year=1982 s.month=11 s.day=11D) s.birth.year=1982 s.birth.month=11 s.birth.day=11 If the birthday is “11 / 11 /82”,which is the correct assignment stateme
20、nt?Access structure members 1 Definition struct student int num; char name20; char sex; int age; ;struct student stu30; struct student int num; char name20; char sex; int age; stu30; struct int num; char name20; char sex; int age; stu30;Array group several related variables with the same type, store
21、 in sequence30 studentsIn one classThe type is structureStructure and arrayThe relationship between structure and array Array is a member of a structure An array which members are all structure variablenumNameScore0Score1101wang9098110zhan8775struct int num ; char name20; float score2; stu1,stu2;num
22、NameScore0Score1101wang9098110zhan8775struct int num ; char name20; float score2; stu2;Structure and arrayInitualizationstruct stud int xh; . ;struct stud xscj3,; OR:struct stud int xh; . xscj3,; Data in one arrayStructure and arrayAccess members of structure arraymain() struct xscj xs4=01,70,80, 02
23、,78,67,03,56,78, 04,90,80; int i; for (i=0;imember-of-structure refers to the particular member.To access the member of structure by pointer(*pointer name ).member namepointer name -member namestruct xscj char *xh; float cj2; float av; xs=02,78,67;main() struct xscj *p=&xs; p-av=(p-cj0+p-cj1)/2; pri
24、ntf(%s,%5.1f,%5.1f,%5.1fn,p-xh, p-cj0,p-cj1,p-av);Structure and pointeroutput:02, 78.0, 67.0, 72.5Points to the structure array main() struct xscj xs4=01,70,80, 02,78,67,03,56,78, 04,90,80 ; struct xscj *p; for (p=xs;pav=(p-cj0+p-cj1)/2.0; printf(%s,%5.1f,%5.1f,%5.1fn,p-xh, p-cj0,p-cj1,p-av); struct
25、 xscj char *xh; float cj2; float av; ;01, 70.0, 80.0, 75.002, 78.0, 67.0, 72.503, 56.0, 78.0, 67.004, 90.0, 80.0, 85.0Structure and pointer1 Structure member as argumentstruct xscj char *xh; float cj2; float av; ;Structure and function main() struct xscj xs4=01,70,80, 02,78,67,03,56,78, 04,90,80; in
26、t i; for (i=0;iav=(tj-cj0+tj-cj1)/2.0; printf(%s,%5.1f,%5.1f,%5.1fn, tj-xh,tj-cj0,tj-cj1,tj-av);01, 70.0, 80.0, 75.001, 70.0, 80.0, 75.0统计十佳运动员选票,在统计之前要将预选的运动员名字赋统计十佳运动员选票,在统计之前要将预选的运动员名字赋给相应的给相应的namename数组,同时将每个人的选票计数器清零。数组,同时将每个人的选票计数器清零。structstruct ports portschar char * *name;name;intint count;
27、 count; x =x =Li Ning,0,Lang Ping,0,Zhu Li Ning,0,Lang Ping,0,Zhu JianJian Hua,0,Lan Hua,0,Lan JuJu Jie,0 Jie,0; ;main( )main( ) intint i; i; for (i=0,i=3;i+) for (i=0,i=3;i+) printf(%s:%dprintf(%s:%dn,xi.name,xi.count);n,xi.name,xi.count); Examples 简单的加密程序。简单的加密程序。可定义一个结构table的成员in,存入输入的字符,out输出加密后
28、的字符。 输入输入(in): a b c d e f g h i j k w x y输出输出(out):c d f a h i k x y w b g j e#include stdio.hstruct table char in; char out; ;struct table trans =a,c,b,d,c,f,d,a,e,h,f,i,g,k,h,x,i,y,j,w,k,b,v,g,x,j,y,e; Examples main( )char ch;int strlong,i;strlong=sizeof(trans)/sizeof(struct table);while(ch=getch
29、ar()!=n)for (i=0;transi.in!=ch & istrlong;i+); if (istrlong) putchar (transi.out); else putchar(ch); Examples struct student int num;char name10;char sex;char *address; ; struct student s3=001,Li ming,M,123 Beijing Road, 002,WangLin,F,130 Shanghai Road, 003,GongJin,M,101 Beijing Road;Examples main(
30、) struct student *p;printf(No. Name Sex address n);printf(-n);for(p=s;ps+3;p+)printf(%5d%-10s%3c%sn,pnum,pname, psex,paddress);Examples num name xb birthday cj1 cj2 year month day 001 Wang structure namestudentstructure membersstructurevariablesstud1struct student char xh3; char *xm; char c;struct student birthday;float cj1;float cj2 ; stud1; stud2stud3stud4stud4;structurearray*st;st=stud;structure pointerDefine Structure