Contenu connexe
Similaire à CS201- Introduction to Programming- Lecture 15 (15)
CS201- Introduction to Programming- Lecture 15
- 2. IInn TTooddaayy’’ss LLeeccttuurree
PPooiinntteerrss aanndd AArrrraayyss
MMaanniippuullaattiioonnss
PPooiinntteerrss EExxpprreessssiioonn
PPooiinntteerrss AArriitthhmmeettiicc
MMuullttiiddiimmeennssiioonnaall AArrrraayyss
PPooiinntteerr SSttrriinngg aanndd AArrrraayy
- 3. Pointers aanndd AArrrraayyss
int y [ 10 ] ; Starting Address of Array y [0]
00
11
22
33
44
55
66
77
88
99
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
- 4. The nnaammee ooff tthhee aarrrraayy iiss lliikkee aa
ppooiinntteerr wwhhiicchh ccoonnttaaiinn tthhee
aaddddrreessss ooff tthhee ffiirrsstt eelleemmeenntt..
- 5. Declaration of aa PPooiinntteerr VVaarriiaabbllee
iinntt yy [[ 1100 ]] ;;
iinntt **yyppttrr ;;
yyppttrr == yy ;;
yptr is a pointer to integer
- 6. Declaration of aa PPooiinntteerr VVaarriiaabbllee
00
11
22
33
44
55
66
77
88
99
y [ 3 ]
[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
- 7. iinntt yy [[ 1100 ]] ;;
iinntt **yyppttrr ;;
yyppttrr == yy ;;
yyppttrr ++++ ;;
- 9. IInn tthhiiss ccaassee yyppttrr iiss aa ppooiinntteerr
ttoo iinntteeggeerr ssoo nnooww wwhheenn wwee
iinnccrreemmeenntt yyppttrr iitt ppooiinnttss ttoo
tthhee nneexxtt iinntteeggeerr
- 11. yyppttrr == yy ;;
iiss ssaammee aass
yyppttrr == &&yy [[ 00 ]] ;;
…………....
yyppttrr == &&yy [[ 22 ]] ;;
- 13. mmaaiinn (( ))
{{
iinntt xx == 1100 ;;
iinntt **yyppttrr ;;
yyppttrr == &&xx ;;
ccoouutt <<<< yyppttrr ;;
ccoouutt <<<< **yyppttrr ;;
**yyppttrr ++++ ;;
}}
EExxaammppllee 33
increment whatever yptr points to
- 23. EExxaammppllee
iinntt yy [[ 1100 ]] ;;
iinntt **yyppttrr ;;
yyppttrr == yy ;;
ccoouutt <<<< yy [[ 55 ]] ;;
ccoouutt <<<< (( yyppttrr ++ 55 )) ;;
ccoouutt <<<< **(( yyppttrr ++ 55 )) ;;
- 27. String IInniittiiaalliizzaattiioonn
cchhaarr nnaammee [[ 2200 ]] ;;
nnaammee [[ 00 ]] == ‘‘AA’’ ;;
nnaammee [[ 11 ]] == ‘‘mm’’ ;;
nnaammee [[ 22 ]] == ‘‘ii’’ ;;
nnaammee [[ 33 ]] == ‘‘rr’’ ;;
nnaammee [[ 44 ]] == ‘‘00’’ ;;
- 29. String IInniittiiaalliizzaattiioonn
cchhaarr nnaammee [[ 2200 ]] == ““AAmmiirr”” ;;
AArrrraayy mmuusstt bbee oonnee cchhaarraacctteerr
ssppaaccee llaarrggeerr tthhaann tthhee nnuummbbeerr ooff
pprriinnttaabbllee cchhaarraacctteerr wwhhiicchh aarree ttoo
bbee ssttoorreedd..
- 30. EExxaammppllee 44
cchhaarr ssttrriinngg11 [[ 2200 ]] == ““AAmmiirr””;;
cchhaarr ssttrriinngg22 [[ 2200 ]] ;;
cchhaarr **ppttrrAA, **ppttrrBB ;;
pprrttAA == ssttrriinngg11 ;;
pprrttBB == ssttrriinngg22 ;;
wwhhiillee (( **ppttrrAA !!== ‘‘00’’ ))
{{
**ppttrrBB++++ == **ppttrrAA++++;;
}}
**ppttrrBB == ‘‘00’’ ;;
- 31. SSttrriinngg CCooppyy
FFuunnccttiioonn
mmyySSttrriinnggCCooppyy (( cchhaarr **ddeessttiinnaattiioonn , ccoonnsstt cchhaarr **ssoouurrccee ))
{{
wwhhiillee (( **ssoouurrccee !!== ‘‘00’’ ))
{{
**ddeessttiinnaattiioonn++++ == **ssoouurrccee++++ ;;
}}
**ddeessttiinnaattiioonn == ‘‘00’’ ;;
}}
- 32. IInn TTooddaayy’’ss
LLeeccttuurree
PPooiinntteerrss aanndd AArrrraayyss
PPooiinntteerr AArriitthhmmeettiicc
MMaanniippuullaattiioonn ooff AArrrraayyss
SSttrriinngg AArrrraayyss