A small presentation on Pointers in C ++

RahulRutton 20 views 14 slides Jul 13, 2024
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

A presentation on Pointers


Slide Content

Pointers
Dr Rishi Ruttun

Learning Outcomes
ComputerMemory
Pointer
WhyPointers?
Pointers’declaration
Pointers’initialisation
Accessingthevalueofavariable
2

Computer Memory
Thecomputer’smemoryisasequentialcollection
ofstoragecells.
Eachcell,commonlyknownasabyte,hasa
numbercalledaddressassociatedwithit.
Theaddressesarenumbersconsecutively,starting
fromzero.
Thelastaddressdependsonthememorysize.
Acomputersystemwith64Kmemorywillhaveits
lastaddressas65,535.
3

Variable and Computer Memory
Eachvariableisassignedamemoryslot(thesizedepends
onthedatatype),andthevariable’sdataisstoredthere.
Variable a’ has a value 100, is
stored at memory location 1010
100… …
1010…
Memory address: 1010 1022
int a = 100;

1007
a
4

Sincememoryaddressesaresimplynumbers(i.e.,1007,1010,1022,etc.),theycanbe
assignedtosomevariablesthatcanbestoredinmemory,likeanyothervariable.
Suchvariablesthatholdmemoryaddressesarecalledpointervariables.
Apointervariableisnothingbutavariablethatcontainsanaddress,whichisthelocationof
anothervariableinmemory.
100… …
10101007
Memory address: 1010 1022

1007
integer pointer
Pointer variable
int a = 100;
Variable a’ has a value 100, is
stored at memory location 1010
Memory
address
pointer
5
a

Why Pointers?
Theyincreasetheexecutionspeedandthusreducethe
program’sexecutiontime.
Theyprovideanefficienttoolformanipulatingdynamic
datastructuressuchasstructures,linkedlists,queues,and
stacks.
Theyareefficientathandlingarraysanddatatables.
.....
6

AddressOperator
Symbol:&(ampersand)
Feature:Givestheaddressofthevariable
DereferencingorIndirectionOperator
Symbol:*(asterisk)
Feature:Givesthevalueofthevariablethatthepointerispointingto
100… …
10101007
Memory address: 1010 1022

1007
integer pointer
Operators in Pointers
int a = 100;
Variable a’ has a value 100, is
stored at memory location 1010
Memory
address
pointer
7
a

Pointer declaration
Sincepointervariablescontainaddressesofseparatedata
types(int,float,double,char,etc.),theymustbedeclared
beforewecanusethem.
Syntaxformforpointervariabledeclaration:
Examples
int*p
oDeclaresthevariablepasapointervariablethatpointstoanintegerdatatype
float*p
oDeclaresthevariablepasapointerthatpointstoafloatdatatype
data_type *pt_name
8

Pointer declaration styles
int*p;//style1//
Int*p;//style2//
Int*p;//style3//
Alloftheabovestylesarethesameinthesensethateach
ofthemisdeclaringapointer.
However,style2ismorepopularasitisconvenientto
havemultipledeclarationswithinthesamestatement:
oE.g.,int*p,z,*t;
9

PointerInitializationistheprocessofassigningtheaddressofavariableto
apointervariable.
Apointervariablecanonlycontaintheaddressofavariableofthesamedatatype.
Theaddressoperator(&)isusedtodeterminetheaddressofavariable.
E.g.,ptr=&a,whereptrisapointervariableandaisavariable.
100… …
10101007
Memory address: 1010 1022

1007
integer pointer
Pointer Initialisation
int a = 100;
Variable a’ has a value 100, is
stored at memory location 1010
Memory
address
pointer
10
a

Pointer Initialisation –E.g.
11
C language

Wecanaccessthevaluestoredinthevariablepointedtobyusingthe
Dereferencingoperator(*).
Let’sgoforanexampleonthenextslide.
100… …
10101007
Memory address: 1010 1022

1007
integer pointer
Accessing the value of a variable using
Pointer
int a = 100;
Variable a’ has a value 100, is
stored at memory location 1010
Memory
address
pointer
12

Accessing the value of a variable –E.g.
13
C language

Summary
Thecomputer’smemoryisasequentialcollectionofstorage
cells,andeachcellhasanumbercalledanaddressassociated
withit.
Eachvariableisassignedamemoryslot,andthevariable’s
dataisstoredthere.
Apointervariableisavariablethatcontainsanaddress.
Pointersmustbedeclaredbeforewecanusethem.
Pointerinitializationistheprocessofassigningtheaddressofa
variabletoapointervariableusingtheaddressoperator(&).
Adereferencingoperator(*)isusedtoaccessthevaluestored
inthevariablepointedto.
14