Professional Documents
Culture Documents
T-I
VPOI
NTERSAND
FUNCTI
ONS
Sy
ll
abus:
Poi
nter
s – De cl
arat
ions,Ini
ti
ali
zat
ion, Poi
nter Ar
it
hmeti
c, Memor
y
all
ocat
ionf
uncti
ons,Arr
aysandPoint
e r
s,Lval
ueandRval
ue.
Funct
ions – De signi
ng Struct
ured Pr ogr
ams ,Us er-
d ef
ined Functions,
Standa r
dF unct
ions,Par
ame terPa ssi
ngTe chniques,Pa ssi
ngAr r
ayt o
Fun ct
ions,
PassingPoint
erstoF unct
ion,Recur
sion,Storageclasses.
Pr
obl
ems olv
ing–Us ingfunct
ionsprintt
hes um ofallel
eme ntsoft
hea rray
usi
ngpointer
s,conver
tdecimalnumbe rtobinarynumbe rus i
ngfunct
ion,
cal
cul
atetheGCDoftwonon- negat
ivei
nteger
susingrecur
sion.
Poi
nter
s:
Point
eri
son eofthemostpower
fulf
eat
uresofC-
language,whi
chhave
numberofadvant
ages.
Point
erse nabl
eust
oaccessav
a r
iabl
ethati
sdef
inedout
side
thefunct
ion.
Thisi sb ecausethe me morya ddr
essesa regl
obaltoall
funct
ions,wh er
easlocalvar
iabl
en amesa remeani
ngf
ulonl
y
wit
hinthefuncti
oninwhichtheyar
ed ecl
ared.
Poi
nter
sreducet
helengthandcompl
exi
tyofa pr
ogr
am. i
.e.
.,
poi
nter
sar
eusedforsavi
ngmemory
.
Point
e r
sprov
ideawayt
oret
urnmor
ethanonev
a l
uef
rom a
funct
ion.
Point
erscanbeusedt
opassar
raysandst
ri
ngsmor
econv
e ni
ent
ly
fr
om onefunct
iont
oanot
her
.
Poi
nter
sincr
easet
hepr
ogr
amexecut
ionspeed.
Poi
nter
spr
ovi
dedy
nami
cmemor
yal
locat
ions.
Decl
arat
ionofpoi
nter
s:
Poi
nteri
sav
a r
iabl
ethathol
dst
headdr
essofanot
herv
a r
iabl
e.
Whe nwedecl
areav ar
iabl
e,anappr
opri
atememory l
ocati
on is
al
locat
edtot
hevar
iabl
ebyt hecompi
lerto hol
dthe v
a l
ue of a
vari
abl
e.
Thisme morylocati
onwil
lhaveitsowna ddr
e ss.
oCons i
derthestat
eme nt
,inti=10;
Thi
sstat
eme nttel
lst
h eCcomp i
lert
op er
formthef ol
lowingact
ions.
1) Reser
vesas paceinmemorytoholdt hei
ntegervalue.
2) Assoc i
atet hen ame ‘i’witht his memor
y
locati
on .ilocati
onn ame
10 v alue atlocat
ion
65514 locat
ion
address
Nowwema ya ccessthev al
ue’10’byusingeit
hert
hename‘i’ortheaddress
65514.Si
n ceme mor ylocati
ona ddressesaresi
mplynumb ers,t
heyc an
bea ssignedt os omev ar
iabl
eswh ichc anbefor
c edinme mory
,like
anothervariable.Suchv ari
abl
est hatholdme mor
ya ddr
e ssa r
ec al
led
point
ers.
Decl
arat
ionofpoi
nter
:
Sy
n t
ax: dat
aty
pe*
var
iabl
ename;
Li
keanotherv
a r
iabl
es,
p oi
nter
smustbedecl
aredbef
oret
heycan
beused.
Datat
ypeisthetypeofthepoi
nter
.Itspeci
fi
est
het
ypeoft
heobj
ect
thatt
hepointercanpoi
ntto.
Ex :i
nt*x;
Theabov
estatementst
atest
hat‘
x’i
sap oi
nterv
a r
iabl
ean dis
goi
ngtocont
aintheaddr
essofavar
iabl
ewh i
chisofintegert
ype.
Wec anreadas“
xisapoi
ntert
oint
”(or
)“xpoi
ntst
oanobj
ectoft
ype
i
nteger
”.
Si
mil
arl
y,f
loat*
a;
I
tdecl
ares‘
a’asapoi
ntert
oaf
loat
ing-
poi
ntv
a r
iabl
e.
I
nit
ial
izat
ionofPoi
nter
s:
Poi
nter
sshoul
dbei
nit
ial
izedei
therwhent
heyar
edecl
aredori
nan
assi
gnment
.
Aval
u ecannotbeassi
gnedt
oapoi
nterv
a r
iabl
edi
rect
lyasan
or
dinaryv
a r
iabl
e.
I ns teadi tmu stbea ssi
gnedava l
ueofav
a r
iabl
ewhi
chisa l
ready
d eclare d.
Synt
a x:d a t
aty pev ariabl
en ame1;
dat at
y pe* variabl
ename=&v ar
iabl
ename1; /
*Ini
ti
ali
zati
onat
decl
arationt i
me */
(or)
dat atype* variablename ;
variablename=&v ari
ablename1;
E x:i n ta;
i
n t*b= 4a ;
(or)
i
nta;
i
nt*b;
b=4a;
I
nthea bovee xampl
e,‘
a’i
sa nordi
naryint
egerv
a r
iabl
eand‘
b’i
sa
poi
ntert
h atpoi
ntst
oa nint
egerv
a r
iabl
e‘a’
.
Thea ddressofthevar
iabl
e‘a’i
sassi
gnedtopointer‘b’byusing&
(addr
es soperat
or)t
hatme anthev
a r
iabl
e‘b’hol
dst hea ddr
essof
vari
able‘a’
.
Apoi
ntermaybei
nit
ial
izedt
o‘0’
(zer
o),
NULLoranaddr
ess.
Apoi
nterwi
tht
hev
a l
ueNULLpoi
ntst
onot
hing.
I
nit
ial
izi
ngapointer‘
0’(zer
o)i
sequi
val
entt
oini
ti
ali
zi
ngapoi
ntert
o
NULL,butNULLispref
err
ed.
Ccont
ainst
wospeci
aloper
ator
s:‘
&’and‘
*’
.
The‘
&’operat
orhasbeenusedt
odenot
etheaddr
essoft
hev
a r
iabl
e
i
nthescanff
unct
ion.
Thisoperat
orisk nownasaddressoper
ator(
or)Unar
yoper
atorthat
ret
urnstheaddressofthev
a r
iabl
eitpr
ecedesanditi
snotr
estr
icted
tothescanffunct
ion.
Itma ypr ecedeonlyav ari
abl
ename( or
)a r
rayelement
, nevera
cons t
ant(or)expr
essi
on.
Forexamp l
e,inti=10;
i
nt*ipt
r;
i
ptr
=&i; ( 1)
(
1)Statementa ssignst
h eaddressof‘
i
’topoint
ervari
able‘
i
p t
r’
.Vari
abl
e
‘
ipt
r’i
ss ai
dto“ point
stoi”.
Bef
oreapoi
nteri
sini
ti
ali
zed,i
tshoul
dnotbeused.
The‘*’operat
or,commonlyrefer
redt
oa st
heindi
rect
ionoperat
or(
or)
deref
e renci
ngope r
ator
,ret
urnsthev
a l
uest
oredatt
hea ddr
essthati
t
precedes .
o Forexample,
pri
ntf
(“%d”
,*i
ptr
);
Theabovest
a t
ementpr
int
stheval
ueofthevar
iable‘i
’
,namel
y 10.
Usi
ng‘*’
int
hismanneri
scall
edder
efer
enci
ngapoint
er.
The‘
*’oper
atori
sal
socal
led“
val
ueataddr
ess”oper
ator
.
Ther
efore‘
*i
ptr
’and‘i
’bot
hrepr
es ent
sthesamedat
ait
em[
i.
e..
,the
cont
entsoft
hes amememorycel
l]
.
Accessi
ngav
ari
abl
ethr
oughi
tsPoi
nter
:
Onceapoint
erha sbeenassignedtheaddr
essofavariabl
e,wec anaccesst
he
vari
ableusingthepointerbypl aci
nganindi
rect
ionope r
atorbefor
e the
point
er.
Consi
derthefoll
owingstat
eme n t
s,
i
nta,*aptr,b;
aptr
= &a;
a=5;
b=*aptr;
Thefi
rstl
inedecl
ares‘a’
and‘b’asint
egerv
a r
iabl
esand‘
p’asa
poi
ntervari
abl
epoint
ingtoanint
e ger
.
Thesecondl
i
neassi
gnst
headdr
essof‘
a’t
othepoi
nterv
a r
iabl
e‘apt
r’
.
Thet
hir
dli
neassi
gnst
hev
a l
ue‘
5’t
o‘a’
.
Thef
our
thl
i
n econt
ainst
hei
ndi
rect
ionoper
ator
s.
Whent heope r
ator‘*’isplaced befor
eapoi nt
erv ari
able in a n
expr
e ssi
on(ontherighthands i
deof t he equals i
gn),
the poi
nter
r
eturnstheval
ueofwhi cht
hepointerval
ueisaddress.I
nthiscase,
*apt
rr etur
nsthev a l
ueoft hev ar
iabl
e‘a’
.Be causea pt
rist he
addressofquanti
ty.
The‘
*’canber
emember
edas‘
val
ueataddr
ess’
.
Thust
hev
a l
ueof‘
n’woul
dbe‘
5’
.
Exampl e:
#inc l
ude<stdi
o.h>
#inc l
ude<conio.h>
voidmai n()
{ intx,y ;
int*pt r
;
x=10;
ptr=&x ;
y=*ptr;
pri
ntf(
“valueofxi s%d”,
x);
pri
n t
f(“
%di ss tor
e dataddress%u\
n”,
x,
&x );
pri
n t
f(“
%di ss tor
e dataddress%u\
n”,
*&x,&x);
pri
n t
f(“
%di ss tor
e dataddress%u\
n”,
*ptr,ptr
);
pri
n t
f(“
%di ss tor
e dataddress%u\
n”,
y,
&* ptr)
;
pri
n t
f(“
%di ss tor
e dataddress%u\
n”,
ptr
,&p t
r);
pri
n t
f(“
%di ss tor
e dataddress%u\
n”,
y,
&y );
*ptr
=25;
pri
ntf(
“\nnowx =%d\n”,
x);
}
Out
put
:
Val
ueofxis10
10i
sstor
edataddr
ess4104
10i
sstor
edataddr
ess4104
10i
sstor
edataddr
ess4104
10isst
oredataddr
ess4104
4104i
ss t
oredataddr
ess4106
10isst
oredataddr
ess
4108Nowx=25
Poi
nterOper
ati
ons:
Poi
nterexpr
essi
onsandpoi
nterar
it
hmet
ic:
I
ng ener
al,poi
nter
sma yb eusedli
keotherv
a r
iabl
es.So,
p oi
nter
scanbe
usedinexpressi
ons.
Forexample,assumingthedecl
arat
ions.
intp=5;
intq=7;
int*xptr
,*ypt
r,
Z1,Z2;
Thef
oll
owingstat
ement
sar
eval
id.
xpt
r=4p;
y
ptr
=4q;
Z1=(
*xpt
r)*(*
ypt
r);
*
xpt
r=*
xpt
r+3;
Z2=xpt
r-
ypt
r;
*
ypt
r=*
ypt
r-
1;
I
na ddi
ti
ontot he‘
*’and‘&’oper
ators,t
her
ea reonlyfourot
her
ar
it
hme t
icoper
ator
sthatmaybea ppl
iedtopoi
nterv
a r
iabl
es+,
++,
-and-
-.
Apoi nt
erma ybei ncr
eme nted(++)orde cr
e ment
ed( --
),ani nteger
ma ybea ddedtoapoi nter(
+or+=) ,ani nt
egerma ybe s ubtracted
f
rom apoi nter(-or-=),orone pointer ma y be subtr
act
ed f rom
anotherpoint
erprovi
dedt hatt
heya r
epointi
ngtothesamea rr
ay.
Pointerar
ithmeti
cd i
ff
ersfr
om “nor
ma l
”ari
thmeti
cinthema nner
thatiti
spe r
for
me drel
ati
vetot
heb aset
ype(suchasi
nt,
float
,char
etc.
.)ofthepoi
n t
er.
Eachti
meap oi
nteri
sincrement
ed,i
twil
lpoi
nttothenextit
em,
a s
def
inedbyi
tsbasety
p e,
b ey
ondtheonecur
rent
lypoi
ntedto.
Forexampl
e,assumet
hatani
ntegerpoi
ntercal
ledpt
rcont
ainst
he
addr
ess2000.
Aft
erthes t
atement
,p t
r++;Execut
es,ptr++ wi
l
lhavet
hev
a l
ue
2002,assumingi
nteger
sa r
etwob ytesl
ong.
I
fthesy
stem usedf
ourby
test
ost
oreani
nteger
,then pt
r++wi
l
lhave
t
heval
ue2004.
Si
milar
ly,i
fanintegerpoi
ntercal
led‘
y’cont
ainst
headdr
ess
4104,
thent
hest
atement
y
+=2;
Woul
dproduce4112(
4104+2*
4)assumi
ngani
ntegeri
sst
oredi
n4
by
tesofmemory.
Thest
atementy
- =2,woul
dsetbackt
o4104.
I
nge neral
,i
ncrementi
ngapoint
erusi
ngthe++ operat
or,i
ncrements
theaddressi
tstor
esbyav al
ueequalt
os i
zeof
(ty
pe)wher
etypeisthe
dataty
peofthev ar
iabl
epoi
ntedto
(
i.
e..
,1by
tef
orchar
,2by
tesf
ori
nt,4 by
tesf
or
f
loat
).
Wema ynotusepoi
nter
sinmult
ipl
icat
ion(
or)divisi
on,orma y not
t
aket
hemodulusofapoint
erandtwopoint
erscannotbeadded.
Apoi
ntercanbeassi
gnedt
oanot
herpoi
nteri
fbot
hpoi
nter
sar
eof
t
hesametype.
Other
wise,acastoperat
ormu stbeusedtoconver
tthepoi
nteron
t
her i
ghtofthea ssi
gnme ntt
ot hepoi
ntert
ypeont hel
eftofthe
assi
gnment.
Wec anapplytheincr
eme ntanddecr
eme ntoper
ator
stoei
thert
he
poi
ntsi
tsel
f(or
)theobjectt
owhichi
tpoint
sto.
Forexampl
e,assumet
hat‘
i
’poi
ntt
oani
ntegert
hatcont
ainst
he
v
a l
ue’
3’
.
Thes t
atement(*
i)++;(not
e:parent
hesesar
enecessar
y.)wi
l
l
i
ncrementt
heval
uep oi
ntedby‘
i
’.
Ex:mai n()
{
i
n t*i,
j;j
=3;
i
= &j;
printf
(“\
nAddr
essofj
=%u”,
i)
;
(*
i)++;
Pr i
ntf
(“\
n i
=%uj=%d”
,i
,j
);
}
Out
pu t:
Addre ssof
j=65524I=65524
j=4
Ina ddi
ti
ont oa ri
thmeti
cop erat
ions,poi
n t
erscanbecompared
usi
ngrelati
on aloper
ators.
Howe ver
,pointercompari
sonsonlyma kesenseoft
hepoi
nterr
elat
eto
eachother,
iftheybothpointtothes ameobject.
v
oidpoi
nter
Iti
sas peci
altypeofp oint
eranditcanpointanydatatype.
Thelimitat
ionisthatthepointeddat
ac annotber ef
erenced
dir
ectl
ybyus ing‘
* ’
ope r
ator.
Sinceitslengthi
sa lwaysu ndet
ermi
ne d.
Therefore,
typecasti
ngmus tbeusedtoturnt
hev oi
dpointert
opoi
nter
ofdatatypewhichwec anref
er.
Exampl e:
v
oidmai n()
{
i
nta =5;
doubleb=3.48;
voi
d* vp;
vp=4a ;
pri
ntf(
“\na=%d”
,*(
(i
nt*)v
p))
;vp=ab;
pri
ntf(
“\nb=%d”
,*(
(doubl
e*)
vp))
;
}
NULLpoi
nter
Anu l
lpointerisas pec i
alpoint
ervaluethatpointer
sn owhere.
Themos tst
r ai
gh tf
or wardwa ytogetan u l
lpointeri
nthep rogr
am
i
sb yu si
ngt hep r
e defi
nedc onstantNUL L,wh ichisd efi
nedb y
severalstandardh e aderfil
es,incl
uding<stdi
o.h>,< stdl
ib.
h>a nd
<st
ring.h>
Toinit
ial
izeapoi ntertoa nullpoint
er,the foll
owing code can be
used.
#i nclude <st
dio.
h>
I nt*i
p=NUL L;
Dy
n ami
cMemor
yAl
locat
ion:
Theprocessofa l
locat
ingme morya tr unt i
mei sk nowna s“Dynamic
memorya l
locati
on s”.
Th ee xacts izeofarrayisunk nownu nt
ilt
hec omp i
leti
mei .
e..
.,t
ime
wh enac omp i
lerc omp i
lesc ode wr it
teni nap rogr
a mming
l
a nguagei ntoae xecutabl
efor m.
Th es izeofa narrayyouh av ede cl
a r
ediniti
all
yc anbes ome t
imes
i
n suff
icientan dsome t
ime smor eth anrequir
ed.
Dy na mic me mor ya l
locati
on a l
lows a p rogram t o obt
ain more
me mor ysp acewh il
eru nni
ng( or)tor el
eases pacewh enn ospace
i
sr equired.
Me mor yma nagementfu nct
ionsa reu sedforall
ocati
nga ndfreei
ng
me mor ydu ri
nge xecut
ionofap r
og ram.
Th e me mor y ma nageme ntf unctionsa red ef
inedi n“s t
dli
b.h”
head erfi
le.
Memor
yal
l
ocat
ionpr
ocess:
Globalv
a r
iabl
esandprogr
aminst
ruct
ionsar
est
oredi
nper
manent
stor
agea r
ea.
Localvar
iabl
esar
es t
oredinst
ack.
Theme moryspacebet
weentheset
woregionsi
sk nownas
Heap.Ther
egi
onisusedfordynami
cme mor
ya l
locat
ion.
Localv
a r
iabl
es st
ack
Fr
eememor
y Heap
Gl
obalv
a r
iabl
es
Pe r
ma nent
Pr
ogr
ami
nst
ruct
ions storagearea
Fi
g:St
orageofa‘
C’pr
ogr
am.
Di
ff
erencesbet
weenSt
ati
cMemor
yandDy
nami
cMemor
yAl
locat
ion
BASI
SOF STATI CMEMORY DYNAMI CMEMORY
COMPARISON ALLOCATI ON ALLOCATI ON
Me mor yisa ll
oc a ted Me mor yisa l
l
oc a t
ed
beforethee xec utionofthe duri
n g t
he exec uti
o nof
Al
locati
onTime
program be gins( Duri
ng t
h ep r
og ram.
Comp i
lati
on ).
Me mor yca nb ea l
located
Na t
ureOf Va r
iablesre ma in
Al
locati
on
atan ytimea ndc anb e
perma nentlya ll
oc a t
ed.
r
e l
ea seda tanyt ime .
Memor yArea Itusess tac kf orma nagingI
tu sesh eapf orma naging
UsedIn thes t
aticalloca tionof t
hedy namica l
locationof
All
ocati
on me mor y. me mor y.
Ins t
aticme mor y I
nd y namicme mor y
all
oc at
ion ,onc et h e al
loc ati
on,wh enme mory
ChangeOf
me mor yisa l
loc a t
ed, i
s all
o catedthe me mo r
y
Memor ySize
the si
zec a nb ech ange d.
me mor ysizec a nnot
ch ange.
Ins t
aticme mor y I
nd y namicme mor y
Executi
on all
oc at
ions che me , al
loc ati
on sche me ,
Spe ed exec ut
ioni sf as terthan execu ti
oniss lowe rthan
d y
n amicme mor y stat
ic me mo r
y al
loca t
ion.
al
locati
on.
I
tislesse f
fi
cientt
han
I
tismor ee f
fici
e ntthan
Ef
fi
ciency ad y
n amicall
ocati
on
as t
atica l
location
scheme .
scheme .
I
mpl eme ntat
ionofstat
ic I
mpl eme ntati
onofdy nami c
I
mplementat
ion me mor yal
locati
onis me morya l
loc at
ionis
simp l
e . comp lex.
I
nt hi
st ypeofa ll
ocati
on
Inthistypeofa l
locati
on
me mor yca nb e
Resizi
ng me mor ycannotberesized
dynami call
ye xp andedan d
aftertheinit
ialal
locat
ion.
shrunka s
necess ary
.
Me moryc anbef reedwhe n
Me mor ycannotbereused
i
tisnol onge rneededa nd
Me mory whe nitisnolonger
r
e usedorr eall
oc at
edduring
neede d.
execution.
Use I
tisusedi
nanar
ray. I
tisusedi
nal
i
n kedl
i
s t
.
Memor
ymanagementf
unct
ions:
mal loc()
calloc()
free()
realloc()
mal
loc():
Thena mema l
locstandsfor“me moryal
locat
ion”
.Thef unct
ionma l
l
oc()
reser
vesablockofme moryofspecif
iedsi
zeandr et
urnoftypevoi
dwhich
canbec ast
edi
ntopoi
nterofanyform.
Funct
ionprot
otypeis:
voi
d*ma l
l
oc (
size-
tsi
ze)
;
sy
ntaxf
ormall
oc:
pt
r=(cast
-t
ype*)mal
l
oc(
byt
e-si
ze)
;
Here,“
p t
r”i
sp oi
nterofcast
-t
ype.Thema ll
ocfunct
ionret
urnsap oi
nter
t
oa na reaofmemor ywithsi
zeofb yt
e-si
ze.I
fthespaceisi
n suf
fi
cient
,
al
locat
ionfai
lsandretur
nsNULLp oi
nter
.
Example:
Ptr=(int*)mall
oc (
100*s i
zeof(
int
));
Thi
sstateme ntwill
a l
l
oc at
e200by tes(si
ncet
hesi
zeofi
nti
s2by
tes)andt
he
poi
nterpoi
ntst othea ddressoffi
rstbyt
eofmemory.
cal
loc():
Thenamecall
ocs t
a ndsfor“c onti
guosal
locat
ion”
. The only di
ff
erence
bet
weenmall
oc()a ndc al
loc()i sthat
,mall
oc() a l
locat
ess i
ngl
e block
memorywhereasca l
loc()a l
loca t
esmult
ipleblocksofme moryeachof
samesi
zeandsetsallbyt
estoz er
o.
Funct
ionprot
otypei
s:
voi
d*cal
loc(
size-
tcount
,si
ze-
tel
tsi
ze)
;
sy
ntaxf
orcal
l
oc:
pt
r=(
cast
-t
ype*)cal
l
oc(
n,el
ement
-si
ze)
;
Thi
sstatementwi
l
l al
locat
ec ont
iguousspaceinmemoryforanarr
ay‘
n’
el
emen t
s.Ift
hespaceisi nsuf
fi
cient
,al
locat
ionfai
lsandret
urnsNULL
poi
nter
.
Exampl
e:
pt
r=(f
loat*
)cal
l
oc(
25,
sizeof
(fl
oat
));
Thi
sst
a t
emental
locat
escont
iguousspacei
nmemor
yforanar
ray
of25el
ement
sea chofsi
zefl
oati.
e.,
4bytes.
cal
loc()f
unct
ionisnor
mal
lyusecf
order
iveddat
aty
p esi
.e.
..
,
Ar
ra y
s,St
ruct
ures.
f
ree()
:
Dynamical
lyal
locat
edmemor
ywit
heit
hermall
oc()orcal
loc()doesnotget
ret
urnonitsown.Thepr
ogrammermustusefree()expli
cit
lytorelease
space.
f
unct
ionprotot
ypei
s:
voi
dfree(
voi
d*bl
ock)
;
sy
ntaxf
orfree()
:
fr
ee(
ptr)
;
r
eal
loc():
I
fthepre v
iousl
ya l
l
oca t
edme morybyus i
ngma l
loc()or cal
loc()i s
i
nsuf
fi
cientormor ethans uf
fici
ent.Then,youcanc hangememorysi
ze
pr
evi
ousl
ya ll
ocat
edusingreal
loc()
.
Funct
ionpr
otot
ypeis:
voi
d*real
loc(
voi
d*bl
ock,
siz
e -
tsi
ze)
;
sy
ntaxf
orr
eall
oc():
pt
r=real
loc(
ptr
,newsi
ze)
;
Her
e,“
ptr
”isal
locat
edwi
th“
newsi
ze”
.
Poi
nter
sandAr
ray
s(1-
DAr
ray
s )
I
nCl anguaget
heel
ement
sofana r
raycanbea ccessedt
hrough
i
ndex.But‘
C’al
sopr
ovi
desaspeci
alwayofar
rayhandli
ngt
hrough
poi
nter
s.
Ana r
raynamein‘C’i
sv erymuchl
i
kea poi
nter but t
her
e i
s a
di
ff
erencebet
weeneachother
.
Thep oi
nterisav ar
iabl
ethatcanappearont
hel
eftsi
deofan
assi
gnme ntoper
ator
.
Thear
raynameisac onst
antandcannotappearont
hel
eftsi
deof
anassi
gnmentoper
ator
.
Whenanarr
ayi
sd ecl
a r
ed,t
hecompil
erall
ocat
esaba seaddr
ess
andabl
ockofcont
iguousmemoryl
ocati
onstosui
tthenumberof
el
ement
s.
Thef
ir
stel
ementofanar
rayhast
hei
ndex.
Thea r
raynamewi thoutanindexhass peci
almeaning i
n‘C’
. I
t
repr
esent
stheaddr
es soft
hef
irstel
ementofanar
ray.
Thebaseaddr
essi
sthel
ocat
ionoft
hef
ir
stel
ementi
nthear
ray.
Comp i
leral
sod ef
inesthearr
aynameasaconst
antpoi
ntert
othe
f
ir
stel
ement.
Supposewedecl
areanarrayasf
oll
ows:
i
nta[
5]={
1,2,
3,
4,5}
Thef
ir
stel
ement
sofawil
lbest
oredasfol
l
ows :
a[
0] a[
1] a[ 2] a[3] a[
4]
Baseaddr
ess 100 102 104 106 108
Wecanal
sor
eferel
ement
sofanar
rayasf
oll
ows:
*
a or*
(a+0)r
efer
stot
hez
e r
oet
hel
ementoft
hear
rayi
.e.
..
,1.
Si
mil
arl
y,*
(a+1)r
efer
stot
hef
ir
stel
ementoft
hear
rayi
.e.
..
.,
2and
soon.
Thi
sisi
l
lust
rat
edbyt
hef
oll
owi
ngpr
ogr
am.
A#incl
ude< s t
dio.h>
#incl
ude< conio.h>
voidmain()
{
inti;
intx [5]={1,2,
3,4,
5};
for(i
=0; i<n;
i++)
printf(“
\ n x [
%d]=%d *
(x+%d)
=%d”
,i
,x,
*(x+i
))
;
}
Ou tput
:
x[0]
=1 *(x+0)=1
x[1]
=2 *(x+1)=2
x[2]
=3 *(x+2)=3
x[3]
=4 *(x+3)=4
x[4]
=5 *(x+4)=5
Lv
alueandRv
alue:
Everyexpr
es si
onh asaval
ue.I
nC,anexpr
essi
oniseitheranl
val
ueoran
rval
ue.Butthev aluei
nane xpr
essi
on(
aftereval
uat
ion)canbeu sedi
n
twod i
ff
erentwa y
s.
1.Anlvalueexpressi
onmu stbeusedwheneveri
tisr
ecei
vi
n gaval
uei.e,
i
tisbe i
ngmod if
ied.
2.Anrvalueexpr
essioncanbeusedt
osuppl
yav al
ueforf
urt
herusei
.e,
to
examineorcopyitsval
ue.
Di
ff
erencesbet
weenl
val
ueandr
val
ue
Lvalue Rval
ue
1)suppos eifa=b,lv
a l
ue 1)supposeifa=b,r
val
uerepr
esent
s
represent
st headdress‘a’ contentoft
hea ddr
ess‘
b’
refer
s
2)Itisknowna tcompi l
et i
me 2)I
tisnotknownunti
lrunt
ime
3)lvaluetel
lswheretos torethe 3)r
valuet
ell
swhatistobestor
ed
value
4)lvaluecannotbea ne xpr
e ssi
on 4)r
valuecanbeanexpr
essi
onora
orac on st
an t const
a nt
Theyar
esevent
ypesofexpr
essi
onst
hatar
elv
a l
ueexpr
essi
ons
Expr
essi
onTy
pe Comment
s
1)i
dent
if
ier Var
iabl
eident
if
ier
2)expr
essi
on[
…..
] Ar
rayi
ndexi
ng
3)(
expr
essi
on) Expr
essi
onmustal
readybel
val
ue
4)*
expr
essi
on Der
efer
encedexpr
essi
on
5)expr
essi
on.
name St
ruct
uresel
ect
ion
6)expr
essi
on-
>name St
ruct
urei
ndi
rectsel
ect
ion
7)f
unct
ioncal
l I
ffunct
ionusesr
etur
nbyaddr
ess
Ev
e ni
fanexpr
essi
oni
sanl
val
ue,i
fiti
susedaspar
tofal
arger
expr
essi
oni
nwhi
cht
heoper
ator
scr
eat
eonl
yrv
a l
ueexpr
essi
ons,
t
hent
hewhol
eexpr
essi
oni
sanr
val
ue.
Forexampl
e,a[
2]i
sanl
val
ue.Butwheni
tisusedi
ntheexpr
essi
on
a[
2]+3,t
hewhol
eexpr
essi
oni
sanr
val
ue,
n otanl
val
ue.
Onl
ysi
xoper
ator
sneedanl
val
ueexpr
essi
onasanoper
and.They
ar
eaddr
essoper
ator
,post
fi
xincr
ement
,post
fi
xdecr
ement
,pr
efi
x
i
ncr
ement
,pr
efi
xdecr
ement
,andassi
gnmentoper
ator
.
NOTE:Av
a r
iabl
enamecanassumet
her
oleofei
theranl
val
ueoran
r
val
uedependi
ngonhow i
tisusedi
nanexpr
essi
on.I
nanexpr
essi
on
a=b,‘
a’i
sanl
val
uebecausei
tisont
hel
eftsi
deoft
heassi
gnment
oper
atorand‘
b’i
sanr
val
uebecausei
tison t
her
ightsi
deoft
he
assi
gnmentoper
ator
.