已阅读5页,还剩32页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Structure Xue Qing2009.4 Structure What is StructureDefinitionInitializationAccess the members of structureStructure and pointerStructure and functionStructure and arraynumber, name , score1, score2, score3, score4 What is StructureIn order to deal with the special data which has several member with different data type, it is need to introduce new data type. Array an ordered set with the same data type.10 90 5 6 9 7 3 1int a82 3 6 54 0 8 1int 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 method 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; - invidualArray : float grade20; same type relatedgrade0 grade1 grade2 grade3 What is Structurenum name grade1 grade210 Li 90.5 A11 Liu 80 B12 Wen 88 B. int char float char How can we define related data that have different type ?struct int num;char name10;float grade1;char grade2; ; What is StructureAll variables with the same types can be grouped in a array.wang 98 88 98 87li 87 66 83 67zhan 78 87 75 61liu 90 76 81 72zhao 87 81 74 71All variables with different types can be grouped in a structureContent name age sex addwang 18 m 3-110li 20 f 7-121liu 17 f 7-212zhao 18 m 3-222structure members ( type ,name)Structure variables Definition132A structure is a collection of related variables, any number of type of variables may be included within it.Defining structure,then declaring struct variables.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.Definitionstruct student int num;char name10;float grade1;char grade2; ; Structure is only as structure template, not occupying memory, because no any variable yet.A) Defining structure num name grade1 grade2struct datachar name20;int age;char address30;long telephone; struct struct_name struct_variable_name;struct data wang; struct data li,zhang; DefinitionB) Declaring structure variable name age address telephonename20age20 bytes2 bytes30 bytes4 bytesaddresstelephonename20ageaddress30telephonestruct data char name20;int age;char address30;long telephone;struct data wang;After declaration, computer will assign memory units to them. Definitionmain ( ) 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(float); 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 student int num;char name20;char sex;int age;float score;char addr30;stu1,stu2; num name sex age score addstu1stu2Definition3 .structure s name can be omittedstruct type1 member 1;type2 member 2; typen membern; variable 1, variable2;struct int num;char name20;char sex;int age;float score;char addr30; stu1,stu2; Definitionnum name sex age score addr201 wang M 20 95 Zhongguancun road202 li F 19 80 Guangda GardenInitializationstruct 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 struct_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,19, “200 Beijing Road” ; InitializationHow to use these values? Access structure membersstud1 . grade1=95;stud1 . grade2=A;where “ . ” is calledmember operator.struct_ var_name . member_name struct int num;char name10;float grade1;char grade2 ; stud1,stud2,stud3; scanf ( “ %s , %f , %c, ”, printf ( “ %s , %f , %c , ”, , 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 = 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 within work address Home addresspost addr tel post addr telstruct address int postchar 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, day; ;struct date today; /* stru-var today */printf (“Enter today date:“);scanf(“%d %d %d”, printf(“%d.%d.%dn ”, today.year,today.month,today.day ); datechar sex;struct int year;int month;int day; birth; ;struct student s;A)year=1982month=11day=11B)birth.year=1982,birth.month=11birth.day=11C)s.year=1982s.month=11s.day=11D) s.birth.year=1982s.birth.month=11s.birth.day=11If the birthday is “11 / 11 /82”, which is the correct assignment statement?Access structure members1 DefinitionForm1 struct student int num;char name20;char sex;int age;struct student stu30;Form2 struct student int num;char name20;char sex;int age;stu30;Form3 struct int num;char name20;char sex;int age;stu30;Array group several related variables with the same type, store 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 variablenum Name Score0 Score1101 wang 90 98110 zhan 87 75struct int num ;char name20;float score2; stu1,stu2;num Name Score0 Score1101 wang 90 98110 zhan 87 75struct 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“,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 name1 Points to the structure variablestruct xscjchar *xh;float cj2;float av;xs=“02“,78,67;main() struct xscj *p=p-av=(p-cj0+p-cj1)/2;printf(“%s,%5.1f,%5.1f,%5.1fn“,p-xh,p-cj0,p-cj1,p-av);Structure and pointeroutput:02, 78.0, 67.0, 72.5Declaration of structureDeclaration and initualization of structure variableDeclaration and initualization of structure pointerAccess the structure member by pointerPoints 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 xscjchar *xh;float c
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 桂林报社笔试常见题目及答案分享
- 中考前最后一次班会市公开课获奖课件省名师示范课获奖课件
- 山东省聊城市高唐县第一中学2025-2026学年高一上学期月考化学试卷(含答案)
- 2026年人教版新版数学四年级上册第三单元口算乘法教学设计
- 江苏省徐州市丰县常店镇常店初级中学、师寨镇东渡希望初级中学2025-2026学年七年级上学期学情检测生物试卷(含答案)
- 修笔工班组建设水平考核试卷含答案
- 汽车发动机装调工岗前基础常识考核试卷含答案
- 锻造工常识考核试卷含答案
- 卤菜技能考核题目及答案
- 船舶电焊工风险识别评优考核试卷含答案
- 2026广清经济特别合作区广德(英德)产业园管理委员会招聘雇员6人笔试备考试题及答案详解
- 教学副校长在2026年秋季开学全体教师大会上讲话:新学期开启新征程新使命呼唤新作为
- 2026年学法减分题库和答案
- 新版《医疗器械经营质量管理规范》培训考核试题(含答案)
- 海底捞服务标准化执行指南
- DBJ51∕T074-2017 成都市地铁设计规范
- 上海市2025上海市工业技术学校人员招聘3人(第一批)笔试历年参考题库典型考点附带答案详解
- 2026智算未来绿色先行-数据中心基础设施建设方案
- 2026年1月浙江省高考(首考)思想政治试题(含答案)
- 幼儿园家长工作制度手册
- 2026年及未来5年市场数据中国医药级羟丙基甲基纤维素行业市场发展数据监测及投资方向研究报告
评论
0/150
提交评论