您好,欢迎访问三七文档
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.DayarereferencesatthecomponentsofthedataobjectMyDataMyPT Year,MyPT Month,MyPT Dayarepointerreferencetothesamecomponents.Note,weneedtouseMyPT=&MyDatabeforethisreferencemakesense;i.e.,MyPT Year(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;PtObj xistheintegerxObj.yisthe(integer)addressy;Obj yistheaddressy;++PtObj xincrementsxnotPtObj;(++Pt) xincrementsPtObjbeforeaccessingx;(PtObj++) xincrementsPtObjafteraccessingxPtObj yfetcheswhateverypointsto(aninteger);PtObj y++incrementsyafteraccessingwhateveritpointsto(thisisanaddressoperation);(PtObj y)++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
本文标题:Tutorial on C Language 美国爱荷华大学的计算机科学系英文原版C语言
链接地址:https://www.777doc.com/doc-4444814 .html