资源描述:
《c数组指针指针函数(c array pointer pointer function)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、c数组指针指针函数(Carraypointerpointerfunction)Pointers,arraysandfunctionsofCDrippingwaterwearsthroughastone.language--------------------------------------------------------------------------------Http://www.sina.com.cn15:57YeskyinOctober15,2004.Wen/ChuYunfengBasicexplanation1.Theessenceofapointerisaco
2、mplextyperelatedtoaddress.Itsvalueisthelocation(address)ofdatastorage;theessenceofanarrayisaseriesofvariables.2,thearraynamecorrespondsto(ratherthanpointing)amemory,itsaddressandcapacityremainunchangedinthelifecycle,onlythecontentofthearraycanbechanged.Pointerscanalwayspointtoanytypeofmemoryblo
3、ck,whichischaracterizedby"variable",soweoftenusepointerstomanipulatedynamicmemory.3,whenthearrayispassedasafunctionparameter,thearrayautomaticallydegeneratestothesametypeofpointer.Question:pointersandarraysIheardthatchara[]isconsistentwithchar*a,isn'tit?Answersandanalysis:Therearesomeessentiald
4、ifferencesbetweenpointersandarrays.Ofcourse,insomecases,forexample,thearrayispassedasaparameterofthefunction,Sincethearrayisautomaticallydegradedtothesametypeofpointer,thepointerandarraythatarepassedasfunctionparametershavecertainconsistencywithinthefunction,Butthisisjustaspecialsituation,iness
5、ence,thereisadifferencebetweenthetwo.Seetheexamplesbelow:Chara[]="Hi,pig"!";Char*p="Hi,pig"!";Thememorylayoutofthetwovariablesisasfollows:Thearrayatakesup8bytesofspaceinmemory,whichismarkedbythenamea.ThepointerPrequires4bytesofspacetostoretheaddress,These4bytesaremarkedwiththenameP.Theaddressis
6、almostanywhere,oranywhere,thatis,nullpointers.Atpresent,thisppointsto8consecutivebytesinaplace,thestring"Hi","pig"!".Inaddition,forexample,fora[2]andp[2],bothofthetwocharactersreturnthecharacter'I',butthecompilerproducescodethatisnotthesame.Fora[2],Theexecutioncodestartswiththepositionofa,moves
7、2twobytesbackwards,andthenextractsthecharacters.Forp[2],theexecutioncodetakesanaddressfromthelocationofp,Addtwoonit,andthentakeoutthecharactersinthecorrespondingmemory.Question:arraypointerWhydoweneedtodefinepointerstoarraysrather