资源描述:
《c函数、数组、指针和调试器gdb--指针(c function, array, pointer and pointer gdb-- debugger)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、c函数、数组、指针和调试器gdb--指针(Cfunction,array,pointerandpointergdb--debugger)AddressandpointerPointerisanimportantpartofClanguage.Flexibleuseofpointerscansolvemanyproblems.Atthesametime,pointerisalsoadifficultpoint,evenprogrammerswithrichprogrammingexperiencesometimes
2、feelconfusedwhendebuggingprogramswithpointererrors.Thefirstvariableinaprogram,whichallocatesonememorytothevalueofthevariable,isusedtocompiletheprogram,forexample:Charc='A';Inti=100;Thecompilerallocated1bytesand4bytesofstoragespacetothevariableC,I,respectively
3、.Eachbyteinmemoryhasanumber,thefirstbyteiscompiledas0,thesecondiscompiledas1...Thisnumberistheaddressofmemory.Supposeapointervariablepisdefined:Char*p=&c;Thispointervariable,P,isusedtostoretheaddressofachartypevariable.Weinitializeitinthedefinitionofthevariab
4、les,theaddressofthevariableCtothevariablep,CforvariableCaddress.Thevariablepstoresanaddress,whichisthevalueofthememoryaddressofthevariable.Atthispoint,wesaypointerPpointstothevariableC.Thefollowingstatementusesthevariablep:CharC2=*p+1;Themeaningof"*P"istoobta
5、inthevalueofthevariablepointedbythepointervariablep,thatis,toobtainthevalueofthevariableC'A',sothevalueofthevariableC2is'B'.Note:char*Pdefinesapointertothechartypevariable.Pstorestheaddressofachartypevariable,andthenthe*PisthevalueofthevariablethatthepointerP
6、pointsto,and&cgetstheaddressofthevariableC.Two,thedefinitionanduseofpointerPointersinClanguagearevariablesthatareusedtostorememoryaddresses.Eachpointerhasadatatypeassociatedwithit,whichdeterminesthetypeofthevariabletowhichthepointerispointing.Forexample,achar
7、pointercanonlypointtothechartypevariable.TheClanguageuses*todeclareanidentifierasapointer,andthegeneralformofthepointerdefinitionis:Datatype*pointervariablename;Forexample:Char*pc;Int*pi,p;Double*pd=NULL;Thesecondlinedefinesapointertoanintegervariableandanint
8、egervariable.ThethirdlinedefinesapointerandinitializesittoNULL,whichindicatesthatthepointerdoesnotpointtoanyvariable.Thepointerstothefirstandsecondrowsareundefinedbecausetheyarenotinitial