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
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
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