高级语言程序设计chapter12

上传人:cl****1 文档编号:587347502 上传时间:2024-09-05 格式:PPT 页数:33 大小:902KB
返回 下载 相关 举报
高级语言程序设计chapter12_第1页
第1页 / 共33页
高级语言程序设计chapter12_第2页
第2页 / 共33页
高级语言程序设计chapter12_第3页
第3页 / 共33页
高级语言程序设计chapter12_第4页
第4页 / 共33页
高级语言程序设计chapter12_第5页
第5页 / 共33页
点击查看更多>>
资源描述

《高级语言程序设计chapter12》由会员分享,可在线阅读,更多相关《高级语言程序设计chapter12(33页珍藏版)》请在金锄头文库上搜索。

1、A First Book of ANSI CFourth EditionChapter 12StructuresObjectivesSingle StructuresArrays of StructuresPassing and Returning StructuresUnions (Optional)Common Programming and Compiler Errors2A First Book of ANSI C, Fourth EditionIntroductionEach data item listed in Figure 12.1 is an entity by itself

2、, called a data fieldTogether, all the data fields form a single unit called a recordIn C, a record is referred to as a structure3A First Book of ANSI C, Fourth EditionIntroduction (continued)4A First Book of ANSI C, Fourth EditionIntroduction (continued)A structures form consists of the symbolic na

3、mes, data types, and arrangement of individual data fields in the recordThe structures contents consist of the actual data stored in the symbolic names5A First Book of ANSI C, Fourth EditionIntroduction (continued)6A First Book of ANSI C, Fourth EditionSingle StructuresStructure definition in C:stru

4、ct int month; int day; int year; birth;Reserves storage for the individual data items listed in the structureThe three data items are the members of the structureAssigning actual data values to the data items of a structure is called populating the structure7A First Book of ANSI C, Fourth Edition sp

5、acing of a structure definition is not rigidSingle Structures (continued)8A First Book of ANSI C, Fourth EditionSingle Structures (continued)Multiple variables can be defined in one statementstruct int month; int day; int year; birth, current;Common to list the form of the structure with no followin

6、g variable namesThe list of structure members must be preceded by a user-selected structure type namestruct Date int month; int day; int year;9A First Book of ANSI C, Fourth EditionBy convention the first letter of user-selected structure type names is uppercaseSingle Structures (continued)10A First

7、 Book of ANSI C, Fourth EditionSingle Structures (continued)Initialization of structures follows the same rules as for the initialization of arrays:struct Date birth = 12, 28, 1987;Structure members can be of any data typestruct PayRecord char name20; int idNum; double regRate; double otRate;struct

8、PayRecord employee = H. Price, 12387, 15.89, 25.50;11A First Book of ANSI C, Fourth EditionSingle Structures (continued)Advantage of structures is when the same structure type is used in a list many timesIndividual members can be arrays and structuresstruct char name20; struct Date birth; person;Exa

9、mple: person.name412A First Book of ANSI C, Fourth EditionArrays of Structures13A First Book of ANSI C, Fourth EditionArrays of Structures (continued)14A First Book of ANSI C, Fourth EditionInner braces are not necessaryArrays of Structures (continued)15A First Book of ANSI C, Fourth EditionArrays o

10、f Structures (continued)Without explicit initializers, the numeric elements of both static and external arrays or structures are initialized to 0 (or nulls)An inferior alternative to an array of structures is parallel arraysParallel arrays are two or more arrays, where each array has the same number

11、 of elements and the elements in each array are directly related by their position in the arraysThey are rarely used any more16A First Book of ANSI C, Fourth EditionPassing and Returning StructuresIndividual structure members may be passed to a function in the same manner as any scalar variabledispl

12、ay(emp.idNum)calcPay(emp.payRate,emp.hours);On most compilers, complete copies of all members of a structure can also be passed to a function by including the name of the structure as an argument to the called functioncalcNet(emp);17A First Book of ANSI C, Fourth EditionPass by value Although the st

13、ructures in main() and calcNet() use the same globally defined structure type, this is not strictly necessary (although it is preferable)Passing and Returning Structures (continued)18A First Book of ANSI C, Fourth EditionPassing and Returning Structures (continued)A structure can be passed by refere

14、ncecalcNet(&emp);double calcNet(struct Employee *pt)(*pt).idNum or *pt-idNum19A First Book of ANSI C, Fourth EditionPassing and Returning Structures (continued)20A First Book of ANSI C, Fourth EditionPassing and Returning Structures (continued)21A First Book of ANSI C, Fourth EditionPassing and Retu

15、rning Structures (continued)+ and - can be applied to structures+pt-hours(pt+)-hours(+pt)-hours22A First Book of ANSI C, Fourth EditionPassing and Returning Structures (continued)23A First Book of ANSI C, Fourth EditionReturning Structures24A First Book of ANSI C, Fourth EditionReturning Structures

16、(continued)25A First Book of ANSI C, Fourth EditionUnionsA union is a data type that reserves the same area in memory for two or more variablesunion char key; int num; double price; val;Each of these types, but only one at a time, can actually be assigned to the union variableA union reserves suffic

17、ient memory locations to accommodate its largest members data type26A First Book of ANSI C, Fourth EditionUnions (continued)Individual union members are accessed using the same notation as structure membersTypically, a second variable keeps track of the current data type stored in the unionswitch(uT

18、ype) case c: printf(%c, val.key); break; case i: printf(%d, val.num); break; case d: printf(%f, val.price); break; default : printf(Invalid type : %c, uType);27A First Book of ANSI C, Fourth EditionUnions (continued)A type name can be associated with a union to create templatesunion DateTime long da

19、ys; double time;union DateTime first, second, *pt;Pointers to unions use the same notation as pointers to structuresUnions may be members of structures and arrays; structures, arrays, and pointers may be members of unions28A First Book of ANSI C, Fourth EditionUnions (continued)struct char uType; un

20、ion char *text; double rate; uTax; flag;rate is referenced as flag.uTax.rateThe first character of the string whose address is stored in the pointer text is accessed as *flag.uTax.text29A First Book of ANSI C, Fourth EditionCommon Programming ErrorsAttempting to use structures and unions, as complet

21、e entities, in relational expressionsAssigning an incorrect address to a pointer that is a member of a structure or unionStoring one data type in a union and accessing it by the wrong variable name can result in an error that is particularly troublesome to locate30A First Book of ANSI C, Fourth Edit

22、ionCommon Compiler Errors31A First Book of ANSI C, Fourth EditionSummaryA structure allows individual variables to be grouped under a common variable nameA structure type name can be used to create a generalized structure type describing the form and arrangement of elements in a structureStructures

23、are particularly useful as elements of arrays32A First Book of ANSI C, Fourth EditionSummary (continued)Individual members of a structure are passed to a function in the manner appropriate to the data type of the member being passedStructure members can be any valid C data type, including structures, unions, arrays, and pointersUnions are declared in the same manner as structures33A First Book of ANSI C, Fourth Edition

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 医学/心理学 > 基础医学

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