Tutorial on C Language 美国爱荷华大学的计算机科学系英文原版C语言

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

TutorialonCLanguageProgrammingTeodorRusrus@cs.uiowa.eduTheUniversityofIowa,DepartmentofComputerScienceIntroductiontoSystemSoftware–p.1/64TutorialonCprogrammingCprogramstructure:DatastructureControlstructureProgramstructureIntroductiontoSystemSoftware–p.2/64DatastructuresPredefineddatatypes:integer(int),smallintegers(short),largeintegers(long)realnumbers(float),largerealnumbers(double)characterdata(char)Userdefineddatatypesusingtypeconstructorsarray,record,pointer,fileIntroductiontoSystemSoftware–p.3/64DeclarationsAdataobjectofadefinedtypeTisdeclaredusingtheconstructoftheformTdatawhereTisatypeexpressionanddataisthedataobjectnameExample:intxdeclaresxanobjectoftypeintegershortxdeclaresxanobjectoftypesmallintegerlongxdeclaresxanobjectoftypelargeintegerfloatxdeclaresxanobjectoftyperealdoublexdeclaresxanobjectoftypelargerealcharxdeclaresxanobjectoftypecharacterIntroductiontoSystemSoftware–p.4/64DefinitionsAnobjectofauserdefinedtypeTisconstructedusingoneofthetypeconstructorsstruct,[],*,FILEthattakesasargumentsobjectsofalreadydefinedtypes.AnewuserdefinedtypeTisconstructedusingthemeta-constructortypedefandatypeoratypeconstructorIntroductiontoSystemSoftware–p.5/64RecordtypedefinitionArecordtypeisdefinedusingthestructconstructorfollowingthetemplate:structTypeName{component1;component2;component3;}ComponentsareobjectdeclarationsoftheformTObjName;Note:TypeNameisanabstractionIntroductiontoSystemSoftware–p.6/64RecordobjectdeclarationAnobjectoftypeTypeNameisobtainedbythedeclarationTypeNameMyRecordOnecanputtogetherthedefinitionandthedeclarationgetting:structTypeName{component1;component2;component3;}MyRecord;IntroductiontoSystemSoftware–p.7/64ExamplerecordExampleofarecordtypedefinitionanddeclarationis:structData{intDay;intMonth;intYear;}MyData,*MyPT,MyArray[Max];Note:typeexpressionsareobtainedbycombiningthetypeconstructorsstruct,*,[],inawelldefinedmannerIntroductiontoSystemSoftware–p.8/64ReferencetorecordcomponentsMyData.Year,MyData.Month,MyData.DayarereferencesatthecomponentsofthedataobjectMyDataMyPTYear,MyPTMonth,MyPTDayarepointerreferencetothesamecomponents.Note,weneedtouseMyPT=&MyDatabeforethisreferencemakesense;i.e.,MyPTYear(MyPT):Year.IntroductiontoSystemSoftware–p.9/64MemoryrepresentationofrecordsConsiderthefollowingdefinitionanddeclarations:structexample{intx;int*y;}Obj,*PtObj;IntroductiontoSystemSoftware–p.10/64MemoryrepresentationMemoryrepresentationofObjisinFigure1integeraddressintegerFigure1:RecordmemoryrepresentationIntroductiontoSystemSoftware–p.11/64MemoryrepresentationofPtObjThisisshowninFigure2integeraddressinteger?integerFigure2:PointertorecordmemoryrepresentationIntroductiontoSystemSoftware–p.12/64FactsaboutrecordsTogivefewimportantfactsaboutrecords,assumethatPtObj=&Objhasbeenexecuted.Thenwehave:Obj.xistheintegerx;PtObjxistheintegerxObj.yisthe(integer)addressy;Objyistheaddressy;++PtObjxincrementsxnotPtObj;(++Pt)xincrementsPtObjbeforeaccessingx;(PtObj++)xincrementsPtObjafteraccessingxPtObjyfetcheswhateverypointsto(aninteger);PtObjy++incrementsyafteraccessingwhateveritpointsto(thisisanaddressoperation);(PtObjy)++incrementswhateverypointsto(thisisanintegeroperation);IntroductiontoSystemSoftware–p.13/64ArraydatatypeAunidimensionalarrayofnobjectsoftypeTisdefinedbyTUniName[n]Note,thisisbothadefinitionandadeclarationAbidimensionalarrayofmnobjectsoftypeTisdefinedbyTBidimName[m][n]TheelementiofthearrayUniNameisreferencedbyArrayName[i].Note,0=inExamples:intx[20],structexampleMyArray[100][100]IntroductiontoSystemSoftware–p.14/64ArraymemoryrepresentationTheindicesoftheelementsofanunidimensionalarrayofsizenare0,1,:::,n-1TheelementsofabidimensionalarrayBidimName[m][n]arestoredinmemoryonarow-major,i.e.,theyare:BidimName[0][0],BidimName[0][1],...BidimName[0][n-1]BidimName[1][0],BidimName[1][1],...BidimName[1][n-1]BidimName[2][0],BidimName[2][1],...BidimName[2][n-1]...BidimName[m-1][0],BidimName[m-1][1],...BidimName[m-1][n-1]IntroductiontoSystemSoftware–p.15/64UniondatatypeUnionsarerecordswithvariablefieldslikeinPascalExample:unionUniName{intival;floatfval;char*pval;}uval,*p;Thevariableuvalmayhaveasvalueaninteger,areal,orapointertoacharacter.OnlyoneofthecomponentsisthevalueholdbytheuvalIntroductiontoSystemSoftware–p.16/64ReferencetounioncomponentsTheelementsofaunionarereferencedinthesamewayaselementsofarecord(struct)arereferencedThememoryrepresentationofvariableuvalwillbelargeenoughtoaccommodateanyofthevaluesthatareusedinitsdefinitionItistheprogrammer’stasktoprovideadiscriminantthatwillshowwhatcomponentofaunionisinthevariableuvalatagiventime.IntroductiontoSystemSoftware–p.17/64ExampleofaunionusageThesymboltableentryofasymboltableusedbyacompiler:structSymTabEntry{char*name;intflags;intstype;union{intival;floatfval;char*pval;}sval;}SymTab[MaxSymb],*PtSymTab[MaxSymb];IntroductiontoSystemSoftware–p.18/64ReferencetounioncomponentsSymTab[i].ObjectandPtSymTab[i]Object,whereObject2fname;flags;stype;svalgarereferencestosymboltableelementcomponents.IntroductiontoSystemSoftware–p.19/64PointerdatatypeEveryobjecthasanadd

1 / 64
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功