资源描述:
《C函数、数组、指针和调试器gdb--指针》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、C函数、数组、指针和调试器gdb—指针Thatexplicitlyindicatesthatthecurrentpointerdoesnotpointtoanyvariable・Forpointers,therearetwooperatorsassociatedwiththem,asfollows・&takeaddressoperator・*:takethevalueofthememorycellpointedtobythepointer・Theuseofpointers4to8cases#ineludeInt,main(
2、)Inta=100,B=200;Int*pl二&a,*p2=&b;Printf(〃%d〃,〃%d〃,〃a〃,〃B〃);Printf(〃%d〃,〃%d〃,〃*pl〃,‘'*p2〃);Printf(〃%x〃,〃%x〃,〃&b〃);Printf(〃%x〃,〃%x〃,〃P1〃,〃卩2〃);*pl=*pl*3;Printf(〃%d〃,a);Printf(〃%drT,*pl);Printf(〃%x〃,&a);Printf(〃%x〃,Pl);Pl二&b;Printf(〃%x〃,Pl);Printf(〃%x
3、〃,P2);Return0;}Theprogramrunsasfollows:OnehundredthousandandtwohundredOnehundredthousandandtwohundredBff59be0,bff59bdcBff59be0,bff59bdcThreehundredThreehundredBff59be0Bff59be0Bff59bdcBff59bdcProgramdescription:(1)theprogramfirstdefinestwopointers,Pl,P2,respectively,pointin
4、gtovariablesAandB.Thefirst,secondlinesoftheprogramoutputarethesamebecausethevaluesobtainedby*Pland*P2arethevaluesofthevariablestheypointto.(2)thethirdlineisthattheyoutputthevariablea,B'saddressinsixteendecimalform,andthefourthlineoutputsthevaluesofpointervariablesPl,p2・Var
5、iablePl,P2storestheaddressofthevariabletheyarepointingto.Plstoresthememoryaddressofthevariablea,andtheP2storesthememoryaddressofthevariableB.Thus,theresultsofthethirdand4stalksarethesame・(3)statement*Pl二*P1*3,firstobtainthepointerPlreferstothevariablevalue,thatis,100,after
6、3,theresultsaresavedtothePlpointofthevariable,atthistimethevariableavaluefrom100to300.Therefore,theprogramsixth,7linesoutput300.Thenexttwovariables,theaddressoftheAandthevalueofthePl,areprintedout,andthetwovaluesarethesameasthosewithouttheexecutionof*Pl二*Pl*3statements・(4)
7、thestatementPl二B,theaddressofthevariableBtothepointervariablePl,thenPlandP2pointtothevariableB,thetwopointervariabledhwyisavariableBmemoryaddress,theprogramoutputisconsistentwiththelasttwolines・Three,pointersandarraysInC,pointersarecloselyrelatedtoarrays.Thearraynameisactu
8、allythepointertothefirstelementofthearray.Seetheprogrambelow:Inta[]二{2,4,6,&10};Int*pl二a;