资源预览内容
第1页 / 共33页
第2页 / 共33页
第3页 / 共33页
第4页 / 共33页
第5页 / 共33页
第6页 / 共33页
第7页 / 共33页
第8页 / 共33页
第9页 / 共33页
第10页 / 共33页
亲,该文档总共33页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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, 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 names, 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:struct 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 spacing 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 following 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 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 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;Example: 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 of 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 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 variabledisplay(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 structures 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 referencecalcNet(&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 Returning 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 (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 sufficient 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(uType) 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 days; 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; union 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 complete 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 EditionCommon 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 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
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号