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

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

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

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

×
保存成功