




已阅读5页,还剩29页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
AFirstBookofANSICFourthEdition Chapter12Structures AFirstBookofANSIC FourthEdition 2 Objectives SingleStructuresArraysofStructuresPassingandReturningStructuresUnions Optional CommonProgrammingandCompilerErrors AFirstBookofANSIC FourthEdition 3 Introduction EachdataitemlistedinFigure12 1isanentitybyitself calledadatafieldTogether allthedatafieldsformasingleunitcalledarecordInC arecordisreferredtoasastructure AFirstBookofANSIC FourthEdition 4 Introduction continued AFirstBookofANSIC FourthEdition 5 Introduction continued Astructure sformconsistsofthesymbolicnames datatypes andarrangementofindividualdatafieldsintherecordThestructure scontentsconsistoftheactualdatastoredinthesymbolicnames AFirstBookofANSIC FourthEdition 6 Introduction continued AFirstBookofANSIC FourthEdition 7 SingleStructures StructuredefinitioninC struct intmonth intday intyear birth ReservesstoragefortheindividualdataitemslistedinthestructureThethreedataitemsarethemembersofthestructureAssigningactualdatavaluestothedataitemsofastructureiscalledpopulatingthestructure AFirstBookofANSIC FourthEdition 8 spacingofastructuredefinitionisnotrigid SingleStructures continued AFirstBookofANSIC FourthEdition 9 SingleStructures continued Multiplevariablescanbedefinedinonestatementstruct intmonth intday intyear birth current CommontolisttheformofthestructurewithnofollowingvariablenamesThelistofstructuremembersmustbeprecededbyauser selectedstructuretypenamestructDate intmonth intday intyear AFirstBookofANSIC FourthEdition 10 Byconventionthefirstletterofuser selectedstructuretypenamesisuppercase SingleStructures continued AFirstBookofANSIC FourthEdition 11 SingleStructures continued Initializationofstructuresfollowsthesamerulesasfortheinitializationofarrays structDatebirth 12 28 1987 StructurememberscanbeofanydatatypestructPayRecord charname 20 intidNum doubleregRate doubleotRate structPayRecordemployee H Price 12387 15 89 25 50 AFirstBookofANSIC FourthEdition 12 SingleStructures continued AdvantageofstructuresiswhenthesamestructuretypeisusedinalistmanytimesIndividualmemberscanbearraysandstructuresstruct charname 20 structDatebirth person Example person name 4 AFirstBookofANSIC FourthEdition 13 ArraysofStructures AFirstBookofANSIC FourthEdition 14 ArraysofStructures continued AFirstBookofANSIC FourthEdition 15 Innerbracesarenotnecessary ArraysofStructures continued AFirstBookofANSIC FourthEdition 16 ArraysofStructures continued Withoutexplicitinitializers thenumericelementsofbothstaticandexternalarraysorstructuresareinitializedto0 ornulls AninferioralternativetoanarrayofstructuresisparallelarraysParallelarraysaretwoormorearrays whereeacharrayhasthesamenumberofelementsandtheelementsineacharrayaredirectlyrelatedbytheirpositioninthearraysTheyarerarelyusedanymore AFirstBookofANSIC FourthEdition 17 PassingandReturningStructures Individualstructuremembersmaybepassedtoafunctioninthesamemannerasanyscalarvariabledisplay emp idNum calcPay emp payRate emp hours Onmostcompilers completecopiesofallmembersofastructurecanalsobepassedtoafunctionbyincludingthenameofthestructureasanargumenttothecalledfunctioncalcNet emp AFirstBookofANSIC FourthEdition 18 Passbyvalue Althoughthestructuresinmain andcalcNet usethesamegloballydefinedstructuretype thisisnotstrictlynecessary althoughitispreferable PassingandReturningStructures continued AFirstBookofANSIC FourthEdition 19 PassingandReturningStructures continued AstructurecanbepassedbyreferencecalcNet doublecalcNet structEmployee pt pt idNumor pt idNum AFirstBookofANSIC FourthEdition 20 PassingandReturningStructures continued AFirstBookofANSIC FourthEdition 21 PassingandReturningStructures continued AFirstBookofANSIC FourthEdition 22 PassingandReturningStructures continued and canbeappliedtostructures pt hours pt hours pt hours AFirstBookofANSIC FourthEdition 23 PassingandReturningStructures continued AFirstBookofANSIC FourthEdition 24 ReturningStructures AFirstBookofANSIC FourthEdition 25 ReturningStructures continued AFirstBookofANSIC FourthEdition 26 Unions Aunionisadatatypethatreservesthesameareainmemoryfortwoormorevariablesunion charkey intnum doubleprice val Eachofthesetypes butonlyoneatatime canactuallybeassignedtotheunionvariableAunionreservessufficientmemorylocationstoaccommodateitslargestmember sdatatype AFirstBookofANSIC FourthEdition 27 Unions continued IndividualunionmembersareaccessedusingthesamenotationasstructuremembersTypically asecondvariablekeepstrackofthecurrentdatatypestoredintheunionswitch uType case c printf c val key break case i printf d val num break case d printf f val price break default printf Invalidtype c uType AFirstBookofANSIC FourthEdition 28 Unions continued AtypenamecanbeassociatedwithauniontocreatetemplatesunionDateTime longdays doubletime unionDateTimefirst second pt PointerstounionsusethesamenotationaspointerstostructuresUnionsmaybemembersofstructuresandarrays structures arrays andpointersmaybemembersofunions AFirstBookofANSIC FourthEdition 29 Unions continued struct charuType union char text doublerate uTax flag rateisreferencedasflag uTax rateThefirstcharacterofthestringwhoseaddressisstoredinthepointertextisaccessedas flag uTax text AFirstBookofANSIC FourthEdition 30 CommonProgrammingErrors Attemptingtousestructuresandunions ascompleteentities inrelationalexpressionsAssigninganincorrectaddresstoapointerthatisamemberofastructureorunionStoringonedatatypeinaunionandaccessingitbythewrongvariablenamecanresultinanerrorthatisparticularlytroublesometolocate AFirstBookofANSIC FourthEdition 31 CommonC
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025甘肃陇南城镇招聘公益性岗位人员13人模拟试卷及答案详解(名校卷)
- 2025湖南省气象部门事业单位第三轮公开招聘应届毕业生14人考前自测高频考点模拟试题有答案详解
- 湖北咸宁通城县事业单位招聘考试真题2024
- 2024年驻马店遂平县公益性岗位招聘真题
- 2024年成都市市场监督管理局所属事业单位招聘真题
- 2025江西赣州经济技术开发区退役军人服务中心招聘见习生1人模拟试卷含答案详解
- 2025年南昌市劳动保障事务代理中心招聘统计监测劳务外包工作人员1人模拟试卷有完整答案详解
- 2025河南郑州航空港区某国企梅河芳邻选手村项目招聘12人模拟试卷及答案详解(考点梳理)
- 2025年福建省级机关医院招聘10人模拟试卷附答案详解(模拟题)
- 2025-2026学年度河北省衡水市阜城实验中学高二上学期9月月考历史试题(含答案)
- 2025至2030中国DHA藻油行业发展趋势分析与未来投资战略咨询研究报告
- T/ZHCA 002-2018化妆品控油功效测试方法
- 青年急性心肌梗死诊断和治疗专家建议2025
- 幼儿园中班科学语言《望着月亮吃大饼》课件
- 颞下颌关节肿瘤
- 学生奶采购配送服务方案(技术标)
- 抗生素耐药性对策与新策略-全面剖析
- 建设工程各方安全管理制度清单及法规依据
- 医疗设备备品备件保障方案
- 《人与动物的关系》课件
- 小红书种草营销师(初级)认证考试真题试题库(含答案)
评论
0/150
提交评论