20
Lecture 8 C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: [email protected]

C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: [email protected] Lecture 8 ... - Write

  • Upload
    others

  • View
    12

  • Download
    1

Embed Size (px)

Citation preview

Page 1: C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: anle.wang@uni-saarland.de Lecture 8 ... - Write

Lecture8

C++programingforbeginners

13June2019

Lecturer:Dr.AnleWang&Dr.SergeySukhomlinov

�1

E-mail:[email protected]

Page 2: C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: anle.wang@uni-saarland.de Lecture 8 ... - Write

Lecture8

• Learnaboutpointers;• Learnabouthowtousepointersasvariablesinafunction;• Discoverthepointerrelatedtoarrays(1Dand2D);

• Afterthislecture,youshouldbeableto:- Understandingtheapplicationsofpointers;- Writesimpleprogramswithapplicationofpointers.

13June2019 �2

ContentsofLecture8

Page 3: C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: anle.wang@uni-saarland.de Lecture 8 ... - Write

Lecture8

• Apointerisavariablethatholdsthememoryaddresstostoreanothervariables;• Thismemoryaddressisthelocationofanothervariableswhereithasbeenstoredinthememory;• Itsupportsdynamicmemoryallocationroutines.

12June2019 �3

Whatisapointer?

Page 4: C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: anle.wang@uni-saarland.de Lecture 8 ... - Write

Lecture8

• Avariabledirectlyreferencesavalue;• Apointerindirectlyreferencesavalue;• Referenceavaluethroughapointercalledindirection.(dereference)

12June2019 �4

Whatisapointer?

3

6

9

2000

LocalMemory

variablei

variablej

variablek

2000

2004

2008

IDoftheaddress

i_pointer

Page 5: C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: anle.wang@uni-saarland.de Lecture 8 ... - Write

Lecture8

• *iscalledindirectionoperator;• &isaddressoperator,isaunaryoperatorthatreturnstheaddressofitsoperand.• Intheexpression,pointer1containaddresswherethevariableiisstoredinmemory.

12June2019 �5

Declarationandinitializationofpointer

Page 6: C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: anle.wang@uni-saarland.de Lecture 8 ... - Write

Lecture8

• Pointervariableistheaddressofthevariable.• Youmustassignanaddresstoapointer.Dereferencingapointerthatnotbeenproperlyinitialized,couldcaseafatalexecutiontimeerror,oritcouldcauseaccidentallymodifyimportantdataandallowtheprogramtoruntocompletionprovidingincorrecterror.• Anattempttodereferenceanon-pointerisasyntaxerror.

13June2019 �6

Declarationandinitializationofpointer

Page 7: C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: anle.wang@uni-saarland.de Lecture 8 ... - Write

Lecture8

• Theresultsare

13June2019 �7

Examples

Page 8: C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: anle.wang@uni-saarland.de Lecture 8 ... - Write

Lecture8

• ThreewaysinC++topassargumentstoafunction- callbyvalue;- callbyreferencewithreferenceargument;- callbyreferencewithpointerargument.

13June2019 �8

Pointerandreference

Page 9: C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: anle.wang@uni-saarland.de Lecture 8 ... - Write

Lecture8

• Thedifferencethatcallbyreferencewithreferenceargumentandpointerargument:- Theaddressnamedasareferencecannotbealtered.Butbecausepointerisavariable,theaddressinthepointercanbechanged.- Referencesdonotrequiretheindirectionoperatortolocatethefinalvaluebeingaccessed,whereaspointersdo.- Referenceprovideasimplernotationalinterfaceandareusuallypreferred.Pointersareusedfordynamicallyallocatingnewsectionsofmemoryforadditionalvariablesasaprogramisrunningorusingalternativetoarraynotation.

13June2019 �9

Pointerandreference

Page 10: C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: anle.wang@uni-saarland.de Lecture 8 ... - Write

Lecture813June2019 �10

Pointerandreference

Page 11: C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: anle.wang@uni-saarland.de Lecture 8 ... - Write

Lecture813June2019 �11

Moreexamples

Page 12: C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: anle.wang@uni-saarland.de Lecture 8 ... - Write

Lecture8

• C++treatsthenameofanarrayasconstantpointerwhichcontainsbaseaddress,i.e.addressoffirstlocationofarray.- Anarraynameisapointerconstant;- Thevalueofthepointerconstantistheaddressofthefirstelement;- ifvalisthenameofthearray,valand&val[0]canbeused.

13June2019 �12

Pointerand1D-array

Page 13: C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: anle.wang@uni-saarland.de Lecture 8 ... - Write

Lecture8

• Whenweneedtofindthenextelementofthisarray:

• whenweadd1ofapointer,weareactuallyaddthesizeofdatatypeinbytes.Itdependsonthedatatypeyoudeclared.

11June2018 �13

Pointerandarray

Page 14: C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: anle.wang@uni-saarland.de Lecture 8 ... - Write

Lecture8

• Referencetheindexofthearray- Useindexa[i]forthei-thelementofthearray;- Use*(a+i)whereaisthenameofthearrayandicorrespondstothei-thelement.

13June2019 �14

Pointerand1D-array

Page 15: C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: anle.wang@uni-saarland.de Lecture 8 ... - Write

Lecture813June2019 �15

Pointerarithmetic

*(ptr++) *(ptr++) Incrementspointers,anddereferencesunincrementedaddress.Thiscommandgivesthevaluestoredattheaddresscontainedinptrandthenitincrementtheaddressstoredinptr.

*++ptr *(++ptr) Incrementpointer,anddereferenceincrementedaddress.Thiscommandincrementstheaddressstoredinptrandthendisplayedthevalueattheincrementsaddress.

++*ptr ++(*ptr) Dereferencepointer,andincrementthevalueitpointsto.Thiscommandfirstgivethevaluestoredattheaddresscontainedinptrandthenitincrementthevalueby1.

*ptr++ (*ptr)++ Dereferencepointer,andpost-incrementthevalueitpointto.Thecommandfirstgivethevaluestoreattheaddresscontainedthenitpostincrementsthevalueby1.

Page 16: C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: anle.wang@uni-saarland.de Lecture 8 ... - Write

Lecture813June2019 �16

Pointerarithmetic

Page 17: C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: anle.wang@uni-saarland.de Lecture 8 ... - Write

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

Page 18: C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: anle.wang@uni-saarland.de Lecture 8 ... - Write

Lecture8

• Here(*p)[4]isapointerarraythatcreateanarrayoffourelements,whereeachelementisapointertoacharacter.

13June2019 �18

Examples

Page 19: C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: anle.wang@uni-saarland.de Lecture 8 ... - Write

Lecture8

• Learnaboutthedefinitionofpointers;• Learnabouthowtousepointerstocallthereference;• Learnaboutthepointerand1D&2Darray;

• Self-learning:- pointerarrays;- pointestostrings;

11June2018 �19

Summary

Page 20: C++ programing for beginners · C++ programing for beginners 13 June 2019 Lecturer: Dr. Anle Wang & Dr. Sergey Sukhomlinov 1 E-mail: anle.wang@uni-saarland.de Lecture 8 ... - Write

Lecture8

• Structureandclass;

13June2019 �20

NextLecture