Upload
others
View
12
Download
1
Embed Size (px)
Citation preview
Lecture8
C++programingforbeginners
13June2019
Lecturer:Dr.AnleWang&Dr.SergeySukhomlinov
�1
E-mail:[email protected]
Lecture8
• Learnaboutpointers;• Learnabouthowtousepointersasvariablesinafunction;• Discoverthepointerrelatedtoarrays(1Dand2D);
• Afterthislecture,youshouldbeableto:- Understandingtheapplicationsofpointers;- Writesimpleprogramswithapplicationofpointers.
13June2019 �2
ContentsofLecture8
Lecture8
• Apointerisavariablethatholdsthememoryaddresstostoreanothervariables;• Thismemoryaddressisthelocationofanothervariableswhereithasbeenstoredinthememory;• Itsupportsdynamicmemoryallocationroutines.
12June2019 �3
Whatisapointer?
Lecture8
• Avariabledirectlyreferencesavalue;• Apointerindirectlyreferencesavalue;• Referenceavaluethroughapointercalledindirection.(dereference)
12June2019 �4
Whatisapointer?
…
3
6
9
…
…
2000
…
…
LocalMemory
variablei
variablej
variablek
2000
2004
2008
IDoftheaddress
i_pointer
Lecture8
• *iscalledindirectionoperator;• &isaddressoperator,isaunaryoperatorthatreturnstheaddressofitsoperand.• Intheexpression,pointer1containaddresswherethevariableiisstoredinmemory.
12June2019 �5
Declarationandinitializationofpointer
Lecture8
• Pointervariableistheaddressofthevariable.• Youmustassignanaddresstoapointer.Dereferencingapointerthatnotbeenproperlyinitialized,couldcaseafatalexecutiontimeerror,oritcouldcauseaccidentallymodifyimportantdataandallowtheprogramtoruntocompletionprovidingincorrecterror.• Anattempttodereferenceanon-pointerisasyntaxerror.
13June2019 �6
Declarationandinitializationofpointer
Lecture8
• Theresultsare
13June2019 �7
Examples
Lecture8
• ThreewaysinC++topassargumentstoafunction- callbyvalue;- callbyreferencewithreferenceargument;- callbyreferencewithpointerargument.
13June2019 �8
Pointerandreference
Lecture8
• Thedifferencethatcallbyreferencewithreferenceargumentandpointerargument:- Theaddressnamedasareferencecannotbealtered.Butbecausepointerisavariable,theaddressinthepointercanbechanged.- Referencesdonotrequiretheindirectionoperatortolocatethefinalvaluebeingaccessed,whereaspointersdo.- Referenceprovideasimplernotationalinterfaceandareusuallypreferred.Pointersareusedfordynamicallyallocatingnewsectionsofmemoryforadditionalvariablesasaprogramisrunningorusingalternativetoarraynotation.
13June2019 �9
Pointerandreference
Lecture813June2019 �10
Pointerandreference
Lecture813June2019 �11
Moreexamples
Lecture8
• C++treatsthenameofanarrayasconstantpointerwhichcontainsbaseaddress,i.e.addressoffirstlocationofarray.- Anarraynameisapointerconstant;- Thevalueofthepointerconstantistheaddressofthefirstelement;- ifvalisthenameofthearray,valand&val[0]canbeused.
13June2019 �12
Pointerand1D-array
Lecture8
• Whenweneedtofindthenextelementofthisarray:
• whenweadd1ofapointer,weareactuallyaddthesizeofdatatypeinbytes.Itdependsonthedatatypeyoudeclared.
11June2018 �13
Pointerandarray
Lecture8
• Referencetheindexofthearray- Useindexa[i]forthei-thelementofthearray;- Use*(a+i)whereaisthenameofthearrayandicorrespondstothei-thelement.
13June2019 �14
Pointerand1D-array
Lecture813June2019 �15
Pointerarithmetic
*(ptr++) *(ptr++) Incrementspointers,anddereferencesunincrementedaddress.Thiscommandgivesthevaluestoredattheaddresscontainedinptrandthenitincrementtheaddressstoredinptr.
*++ptr *(++ptr) Incrementpointer,anddereferenceincrementedaddress.Thiscommandincrementstheaddressstoredinptrandthendisplayedthevalueattheincrementsaddress.
++*ptr ++(*ptr) Dereferencepointer,andincrementthevalueitpointsto.Thiscommandfirstgivethevaluestoredattheaddresscontainedinptrandthenitincrementthevalueby1.
*ptr++ (*ptr)++ Dereferencepointer,andpost-incrementthevalueitpointto.Thecommandfirstgivethevaluestoreattheaddresscontainedthenitpostincrementsthevalueby1.
Lecture813June2019 �16
Pointerarithmetic
Lecture8
• Considerpointernotationfortwo-dimensionalnumericarrays;
• Ingeneralnums[i][j]isequivalentto*(*(nums+i)+j).
13June2019 �17
Pointerand2D-array
Pointernotation Arraynotation Value
*(*nums) nums[0][0] 16
*(*nums+1) nums[0][1] 18
*(*nums+2) nums[0][2] 20
*(*(nums+1)) nums[1][0] 25
*(*(nums+1)+1) nums[1][1] 26
*(*(nums+1)+2) nums[1][2] 27
Lecture8
• Here(*p)[4]isapointerarraythatcreateanarrayoffourelements,whereeachelementisapointertoacharacter.
13June2019 �18
Examples
Lecture8
• Learnaboutthedefinitionofpointers;• Learnabouthowtousepointerstocallthereference;• Learnaboutthepointerand1D&2Darray;
• Self-learning:- pointerarrays;- pointestostrings;
11June2018 �19
Summary
Lecture8
• Structureandclass;
13June2019 �20
NextLecture