资源描述:
《C语言教程第十章(英文版).ppt》由会员上传分享,免费在线阅读,更多相关内容在PPT专区-天天文库。
1、Chapter10Structures,Unions,Enumerations10.1Introduction10.2StructureDefinitions10.3InitializingStructures10.4AccessingMembersofStructures10.5UsingStructureswithFunctions10.6ArrayofSructures10.7typedef10.8Example:High-PerformanceCardShufflingandDealingSimulation10.9Unions10.10Enum
2、erationConstantsOutlineStructureDefinitionsInitializingStructuresAccessingMembersofStructuresUsingStructureswithFunctionstypedefArrayofStructuresKeyPoints10.1IntroductionWhyneed“Structure”?Sofar,we’ddiscussedtwokindsofdata:Simple:char,int,long,double,etc.Scalar:array,pointer,etc.
3、It’sinconvenientinsomeapplications:Seenextslidesforanexample10.1IntroductionWewanttorepresenttimeasyear/month/date:intf(){intyear1,year2,month,date;year1=2050;year2=2020;month=12;date=30;date++;/*Shouldweincreaseyear1oryear2?*/}Theproblemisthatthereisnologicalconnectionbetweenthe
4、m.Weneed“structure”!10.1IntroductionStructuresCollectionsofrelatedvariables(aggregates)underonenameCancontainvariablesofdifferentdatatypesCommonlyusedtodefinerecordstobestoredinfilesCombinedwithpointers,cancreatelinkedlists,stacks,queues,andtrees10.1IntroductionStructureStructure
5、isaadvanceddatatypeinC.Structureisamethodforgroupingaseveralrelateddatatypetogether.VariableswithdifferenttypescanbegroupedinastructureThesimplevariables:inta,b;a=20,b=85;-invidualArray:floatgrade[20];sametyperelatedgrade[0]grade[1]grade[2]grade[3]……10.1Introductionnumnamegrade1g
6、rade210Li90.5A11Liu80B12Wen88B….intcharfloatcharHowcanwedefinerelateddatathathavedifferenttype?struct{intnum;charname[10];floatgrade1;chargrade2;};10.1IntroductionAllvariableswiththesametypescanbegroupedinaarray.wang98889887li87668367zhan78877561liu90768172zhao87817471Allvariable
7、swithdifferenttypescanbegroupedinastructureContentnameagesexaddwang18m3-110li20f7-121liu17f7-212zhao18m3-222structuremembers(type,name)Structurevariables10.2StructureDefinitions132Astructureisacollectionofrelatedvariables,anynumberoftypeofvariablesmaybeincludedwithinit.Definingst
8、ructure,thendeclaringstructvariables.Def