您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > c和指针课后题答案(清晰完整版)
PointersOnCInstructor’sGuidePointersonC—Instructor´sGuideiContentsChapter1AQuickStart........................................................................................................1Chapter2BasicConcepts......................................................................................................7Chapter3Data.......................................................................................................................11Chapter4Statements.............................................................................................................15Chapter5OperatorsandExpressions....................................................................................23Chapter6Pointers..................................................................................................................29Chapter7Functions...............................................................................................................37Chapter8Arrays....................................................................................................................43Chapter9Strings,Characters,andBytes..............................................................................55Chapter10StructuresandUnions...........................................................................................69Chapter11DynamicMemoryAllocation................................................................................75Chapter12UsingStructuresandPointers...............................................................................79Chapter13AdvancedPointerTopics......................................................................................87Chapter14ThePreprocessor...................................................................................................93Chapter15Input/OutputFunctions..........................................................................................95Chapter16StandardLibrary....................................................................................................119Chapter17ClassicAbstractDataTypes.................................................................................129Chapter18RuntimeEnvironment...........................................................................................1451AQuickStart1.1Questions1.Tomaketheprogrameasiertoread,whichinturnmakesiteasiertomaintainlater.3.Itiseasiertoseewhatanamedconstantrepresents,ifitiswellnamed,thanaliteralconstant,whichmerelydisplaysitsvalue.4.%d%s%g\n6.Theprogrammercanputinsubscriptcheckswheretheyareneeded;inplaceswherethesub-scriptisalreadyknowntobecorrect(forexample,fromhavingbeencheckedearlier),thereisnooverheadexpendedincheckingitagain.Buttherealreasontheyareomittedisthefactthatsub-scriptsareimplementedaspointerexpressions,whicharedescribedinChapter8.7.Morecharacterswouldbecopiedthanareactuallyneeded;however,theoutput_colwouldbeupdatedproperly,sothenextrangeofcharacterswouldbecopiedintotheoutputarrayattheproperplace,replacinganyextracharactersfromtheprecedingoperation.Theonlypotentialproblemisthattheunboundedstrcpymightcopymorecharactersintotheoutputarraythanithasroomtohold,destroyingsomeothervariables.1.2ProgrammingExercises1.Watchthesolutionsforproperuseofvoiddeclarationsandareasonablestyle.Thefirstpro-gramisnoplacetobeginlearningbadhabits.Theprogramwillcompileandrunonmostsys-temswithoutthe#includestatement./***PrintthemessageHelloworld!tothestandardoutput.*/#includestdio.hvoidmain(void)Solution1.1continued...12Chapter1AQuickStart{printf(Helloworld!\n);}Solution1.1hello_w.c3.Manystudentswillattempttoreadtheinputfilelinebyline,whichisunnecessarilycomplicated.Othercommonerrorsaretoforgettoinitializethesumto-1,ortodeclareitanintegerratherthanacharacter.Finally,besurethevariableusedtoreadthecharactersisaninteger;ifitisacharactervariable,theprogramwillstoponsystemswithsignedcharacterswhentheinputcon-tainsthebinaryvalue0377(which,whenpromotedtoaninteger,is-1andequaltoEOF).Notethattheoverflowrendersthisprogramnonportable,butwedon’tknowenoughyettoavoidit./***Thisprogramcopiesitsstandardinputtothestandardoutput,andcomputes**achecksumofthecharacters.Thechecksumisprintedaftertheinput.*/#includestdio.h#includestdlib.hintmain(void){intc;charsum=–1;/***Readthecharactersonebyone,andaddthemtothesum.*/while((c=getchar())!=EOF){putchar(c);sum+=c;}printf(%d\n,sum);returnEXIT_SUCCESS;}Solution1.3checksum.c4.Thebasisofthisprogramisanarraywhichholdsthelongeststringfoundsofar,butasecondarrayisrequiredtoreadeachline.Thebuffersaredeclared1001characterslongtoholdthedataplusitsterminatingNULbyte.Theonlytrickythingistheinitializationtopreventgarbagefrombeingprintedwhentheinputisempty./***Readslinesofinputfromthestandardinputandprintsthelongestlinethat**wasfoundtothestandardoutput.ItisassumedthatnolinewillexceedSolution1.4continued...PointersonC—Instructor´sGuide3**1000characters.*/#includestdio.h#includestdlib.h#defineMAX_LEN1001/*Buffersizeforlongestline*/intmain(void){charinput[MAX_LEN];intlen;charlongest[MAX_LEN];i
本文标题:c和指针课后题答案(清晰完整版)
链接地址:https://www.777doc.com/doc-5315796 .html