0% found this document useful (0 votes)
492 views

Gate C Notes

1. The document discusses various data types in C including integers, floating point numbers, characters, strings, and Boolean values. It also covers constants, declarations, and type conversions. 2. Precedence and order of evaluation in C expressions is described. Logical expressions are evaluated left to right and evaluation stops when the outcome is known. 3. The differences between call by value and call by address parameter passing in C functions are explained. Call by value passes a copy of the argument while call by address passes the address of the argument.

Uploaded by

Anmol Dubey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
492 views

Gate C Notes

1. The document discusses various data types in C including integers, floating point numbers, characters, strings, and Boolean values. It also covers constants, declarations, and type conversions. 2. Precedence and order of evaluation in C expressions is described. Logical expressions are evaluated left to right and evaluation stops when the outcome is known. 3. The differences between call by value and call by address parameter passing in C functions are explained. Call by value passes a copy of the argument while call by address passes the address of the argument.

Uploaded by

Anmol Dubey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 141

Val:d oti 9 and plus ol minw

Stqn
Eq:- 124, 345,0, +52, -327857
Cnval:
Space, Coma sumbolA
1 00 277
23 356
RS. 458
852
octal starts with 0 using
diqtt o t 4
Eq 056, 0246
hexadecimal starlina with ox
or OX
ustng the digts
o t and OA toF

Ox56, 0XA246, Oxabcd


Xarge integers unsiqned, long and
Unstqned Xonq
tntegers
456820 19999L 50204548+UL
Rea Cenitaims Jracllons
- 3.24
0 025 2 025

+5 +4, 0.241, 241, -0.21, - 21


+ 8, 0.52e 8, 1-25E -12

Real constant

E x c p o n e r i t t a l
(e o y E)
actomal
345-23 E -4
234. 456 e c p o n e r t t a l

poLt

man ttssa
f r a t o n a l

decbna
past
Part
rt
25 X
10
345

Canquage acepts Sloali ng pt


pt
Coms tants either n vactional ox
ezporenttal Joom.
he pointf pints tn
fvaclional
JOm when.fs used as the oamat
Specifter pints in ecpomenltal m
when .e ts used as the fomatat

Specifiex.
int main C)
Sloat x,9,7;
=12615 34;
y= 145.G58E-2
ty
pntf(", );
print("e,z);
vetwn 0

12676 968 75
264680et004

vecedence and order oevaluatton


C ike most Xarquages doesnot specify
the oxde tn ohich Lthe operands a
OperatoY axe evaluated

he excepltoms ave ( wtth quaxanteed


a

left to atgnt
evaluatiom)
&

Comma perator
In a
statement tke
X0+90;
ray be evaluated
OY Vtce Versa
bejore q

Logical expressibns oaye


evalvated rom
eft tozight and the evaluatton ts
stopped
FALSE
aS SOD as
ts establtshed.
theTRUE ()
n t e g e n

ype
d a b a t y p e

loatt pt data
poimau type
char data type

C supported
usey-defened
empy data yes Structuses,\
(Void) Untons

derived
aYous, poùnters
stbyas)
Symbol
chara cter
ettes
digtt
Mumbey
inteqes -I,+273,
sttng "qate"
nt 20.45
prsint Fl,);
when I am unytng his
this on online
COmptleS even wtthout anu
epAeci ype Convers ton i get
=20How?
Here int and fbat have the same
size and theve ts no
expkctt ype
cas&ng dequised fov such compilers.

unay tncTement
Pepast
att

Tenay opealor
?: (3opevands)
If s true, t goes ts y, xy
Tf is false, tt qoes to , X= 7
declasaltom
mo memoy is CYeated
and only type s infomec
to the Comptle
ALL
ALL declaralions ase deleted at vun tEme
by
by the compiles. Declaratons axe not vequlrec
at un me

int
xts qlob
xtakes o as the deault.
tnt
i s ocal
x takes qarage value by desault

mtX=20.42
2bytes compilallon esoY

intx-int)20-4R;
not
Possible)
expkict tupe-caslüng
bu
J.
implictt
20 (tuncation)
type-casing
integes+ tntegeY ntegey
tnteger + loat Aoat
implicit
int eqer x loat loat J
shot = 32161;
= *+\
bytes

pabk , D;
Op -32168

int Cnrt , tt y

int loat y.

pint'qete -1a,d, 1d, 1,3t)


LR machtne
RLJdependent
oyrdex oh
Choosing Valuès
O. qocbe= 2,3,4
Heve is mot an
ope rator
You d t kvow which is Passed
fvst
n t x,y,z, w, S, P'
= \0

=0
Ep= 75,
X-y? w?r:p

ese p: 505 655

3
else

E E2

TfE, s Juu; ten E will be executed


T E ts ze; then E2 nevey executeS

execulion o E2 depends m
E
X=2 ?
3:6? 8: 4

X=
(223 :
(6? 8:2))
not
ue executed

X=3/

a- bg c? d:e: f?9:b

a 7Cc2d:e): (f 19:h))
/
tue

alse

ISbts u 2 will be
executed.

Ibes false, 1 otll be


executed

-( 2,3) E)
3
=, 2,3
= has htghes
pveference than

nt X-T=2
ae Constdexed
here y, z

eso on)
comptlalion)
to be not declase.

int y,
tntt x=Y=3
n =2;

patnt 5("3ate"), paitf (exa );

pstf(gate")
printS("exam");
p:oateexam

pintfCgate-14,1d,1d, 2,3,4);
D gale = 2,3, 4
order o
pa.ssi Voalues
CS mot

i=y=-3
Knowm
3j
9z
- = X=3;
+=, = *=

z =3

tnt , 2,
= 0
3
Z=4)

EXkk (ptf( gate") +z);


p Nothing

aote" will mot be pinted as is


FALSE

Ak 5 l5

23 k 0
0 ollo0
Loaical AND
9Aogical oR
Short circuits
use C now
and tncement just after
thts stalementt.

incvement X now and


use the incremented x
after this statement.

J-t+3X (x++)
3x2 y= 3x3 y= 4x3
6 |2

side effects 12s o/p tn


omltne compilex- How?
X=2, y=3, R=4
a=
++) X
(++y) (z+t);
-

a= 2 X4 -4 =
4
a-4, X=3, y=4, R=5
Statemenls
acoo
dedaxalion statements
2 evession
scanScans.d, ) call by address
patntf(".d",
* l by
Call by valul
value

Call by addreSS

Lnt int c
= pont ( ) ;
=potntf( ");
holds O
holdsi
tntC
x=patntf ("--");
spaces
holds 3
tmt a- 25
pvint (a< 10 ?snall:
"qreatt
9 eat

int a-25;
Pointf ("d, a<10? 20:30);
vetuan 0
930
Polt("gate exam);
Qateexam

paitCgateexai)
op qateexam
x=pintf C1a", pintf(gate-4 pantf(«))=
pattF(',
Op Cgate=1G|
it,9,3
= SCanf Cd1d", &ac,&y);
pnt5 (p&", z);

int
Scam (a",);
No compiaWon en0T
Seqmentaim Jaut(un tme cuy)
Votd main ()
t , ji
uhtle (to)

while (j<=10) j*=2,


-2

3
Op0,16
nt =3,
do

pivt(, );
3whtle (cio) thEstRs semicolon
thesemicolm
ts end o
do-uwhtle
int X-3;
do

printf (1d, x);


while (xao);
Iinite tEmes 3 will,be
paint ed
int
loop
whtle (xe1o); infinite

pstnt(.d, x);

p NothEng

if); COmpiler eor


whde C); J
t(1);
whtle (4); Mo COmpile lime emor

Ln sun tme tnfinile Roop

tnstntte Koop
sy desault assumed as
TRUE

dox( expvession; expression2; expvesston3)

Statenments
o.);
0

while (o, paint("gate);


Covnsideved Tnfentte tEmes
gate wtll be potnitedl&
wuhile(.o) peint f(aate');
opethina
S wtll be_execatedd ony
ase Once
dorcp, p2, e3
S2
T u

3
$3

int i
JUr (t-0 iL5, i=it+)
pointfd" i);
p0 2 3 4
alse
for (0; 0,o)
pit fCgat);
pzit5 ("exami);
p: exam
Ju
Sos (1 ; 1)
paintfC'30te);
J

pwitexam);
pIvftntte tmes qate ull be painted

int X=\,
false
Jor(X-0 K-o; X=0)
poirtS Cgate);
oNothg
fos (tvt jso; jew 3; j++)
Cj4)
potntS("&,j):
O
p 2 3 56 8

Covill nue it wll skip all the


Yemaininq statements
theAnop and sends cottrok
to the beqiming othe
Koop

goto abc
abe
pabt(xy); tn he
Sae Jm
umcmdiEonal jummp
unctiomS

Svmoduaily
Yeadalsilutg, Small
unslandable, se-usable
Uncon
Atvaryfumellons
use dehined
(pe-detned) un cltons
ExPaintf
potnt, scant,
prototy pes available tn stdio.hb
deftnilions avai lable in

dunamic Acbra
Ainked Xbrarte sS

Junclan usea deftned

pooto ty pe defenilon call


(dedaalion (me antng Cusage
dunclEnally)

unclton mame
Juunclion axquments (name, tupe)
etum type

dunclion pototy pe (Hunclin declaaaltem


ent unfPloat);
tnt fun (floot ) optena
Settun Juncliom arqumertCs)
name
type
uncton dejinilion:
int n(float a) name O
the yaoiable
Stateenls
yetusm
Tetuun
3 the
type Juncliem
name
Vasiable

etuan is opltomal you domt want|


to etwn.
o-va
Not Name ats Compulsoy Jor
definittom whereastt s mot
Compul soy sor pooto ty pe/ declaralibn.

No memovy ts cieated for paototype o


dedaxalto whereas memoy sS
Cveated Sor definttEon
OLD stule
NEW style
void (votd);
Votd matnC) Votd f )

pintfCgate)
3 J
Void matn)
Void f)

pontCgate) 3
Junctons
with/wtthout
wtthui thout xqumenlS
setun Value

Guncttom with aquments and with retum


Value (populas
tt) Junclton wtth axqumenlS andl mo etun value
ti Sunlton wtthout axqumevts and wtth vetun
Value
SuncEion without axqumeits and no vetum
Value

Addillon o nuumbers
with et
wtthout wthout
wtthoutset wtth arq
wtthout axq with asq with vet
without aq

with axquments poweijul

without
axq and
withoutetum Value
Void sum)
tnt
point ("enter 2 numbers"
Scanf(1 , *x, *y
pintSum=1d, xty);
Void maln()
sumC);

wth avq and without vetun value


Sum
void (tnt tnty)

pintssum= d, Xty);
Votcl main )
nt a, b
paint ("entr mumbens);R

scanf("d&1d", ka, 4b)/


Sum ( )
3
a,b ane
pass Values
tii th set and uthout aug
int Sunm )

t
X= 10, y= 20
xetuan(tY)
3
Vodl mainC)
pintf C"d', sum());

v)wth aq and with vetusm value

int Sum(tnt &nt

setumxy)
Votd matnC)

int a-20, b= 30
tnt C
C= Sum(á, b)

paont5 Cd, c);


int (nt x, tnt y )
callee

aeJoYmal axqumenls
3 Clocal)
matn )

tnt a,b
ab axe
cla,b) actual
ayquments
calle

Here, S is called " called fn"

and maln ts called "calltng n


axamate passtnq techmiq ueS in C

Call by Value callbyvetexencee


pass s ualue) pass by. vEdesence)
stidt evaluattom
call by Value lactuals and, Jomal S mainlatn
a diffeent copy
in t
ComlerVoid tnt x, int memoy

yaints C1&, y); J20

OS Votd main)
int a-10, b=20
b20
Sa b);

At
Atun TEme, execulim beains fiom
votdmain().
Compile tme tt compiles Jrom the
baqimiymg

mal axquments camot pdate the actual


asqumermts

call oy vefevence ae potnlers whtch


P.9 t,b
have addresses o
votd (tt wp, tnt *a,) Po00
nt
004
X =

(*p) + (*4)
pointSC1, );
Votd main()
tnt a =10, b-20
a1 OOO

&(ka ,xb);
20

I004-

to
Ps potntna to and 9, cs potnttq to b.

Neuw, 4als cay update the actua 4


they waTt

tnt
vetum (y+);
3
Votd atnC)

xF(x+);
inta (*4, x);

Op 202
t f ( t (nt J)
oetum CJ+t);
3
Void main C)
int C=10
y-f(xt+)
paent5(x= 14A,y-td, x,9);
3
olg
Op X= 11, y= 10

tnt S(tnt y)
vetun (t+g);

Void main )
int-aJ=20
)
pakntF Cd, 1d", x,y);
10, 12
evey Junctton is an tdeniJier
Yame

eve Junclien defition mustbe


qNobal/ ecleml. Don'+ defie a
functEon wtthty anothey dunclio
a l l funclions ave external Joy
default.
Yetun and exitl) ae the way
to teminate Junclien and pbDq vam
espectivelu
Two Junclioms can have same Yame ?

StovageClassesS

Vaiable Funclioom

nome S the
mome aiven Lo
Vauabe
tne memOs
Yame qiven
to the memoy ocalLin at
localim un-Wme,
at un-lUme

Vabiabe holds data wheveas Suncltom


has is owm Code
Vacsiabes uncton

9Mobal by default t
ocal sqkoba/extemal

stovage classes

A
Vstoraqe Aocatiom
Storaqe SCOpe
iTialisoalion

veqiatermatn
/mem0sU
dusaticm / aCcesSS defaut
Vistbilt Cnttta

(tHous long Value

avalale in block scope data stad


)
gahage seg
rememoiuy? (rthin block) seg
funclian SCop
Aulattine (uwtthtn Junctton)
Pooqam SCope
(wilhin proqram Voouables
Files scope
(LIFO) >nita\ cthtn &le)
stack vegion automattcally CYeated and deletec
depends on (allocate) Aeallocate
scopeCblock/functiom
tnttra
data veqión Valu zED
TL the poqxam tevmirmales memoy tS
available
Stora qe classes

extem
auuto 3eqisley
static

auto
auto
declaxalion
L Aocal vasualwthtn block| unctton
matnC)
bnt
t a u t o tt ;
int y; //auto tnt y
X=10, y= 20
pnintS("d,|.d", x, y);

y memly deleted stack


automalk caly
The tnilal values o xy aie
memoy gasbage.
deleted
aulomoltcalls

int =20
ptntfd, <);
Point S(d", x);
3 2000

z[iol1om
stack

auto (always Aoca

)iletzme wtthtn Mock

scope/vstbelty accesstbility
Rimtted ts block
3 default tnital alue
gavbage
stosageXocalton stack
memo place)
always
seqister Local
segtster tnt t t s used.
Jor(l-0 t<1O00 i++) dreguen
ty

(ot yun ttme)


eqisler is Speeder compared ts
stack
So, betie to decdare'as seqtsles.

eqtsler

Aieime thin block


2 Scope kimitad to block
de fault tnttal valuegarbage
stosaqe Xocaltom > scatsler stack
Posstble

If the eqislersS ave mot


dree, OS
gves pemi3a ion tb stack.

automaltc vastables auto

eqtster

3statie
Can e used as Aocal qkobal
memo sy Cveatedin
data veqim
derault tnittal value zeo
stalic ont x;
Aetime t l l the end o pvam
Memoy cxeated for thRS just beore
the poogsam execu llon egins
and'stays trll the end
Proqram in the memoy whether O.
,you use it ox mot 0
SCOpe depends om decl axalton

Ent = 0
nt=
while (x4=i) while (x4 =)
tnt t-o stattc nt i=0;

paintf("", i); psint(.8, i);


3

0 12

Aoqically
mot poeSent
CPsoqammia, 2

data

user deked
paimttive
dei ved data
data
data
aay
potnter
StuctuYe
2. Untoym
i n t (20r4 utes)2
3. enum
2- chas( byte)
3. float(+ btes)
Covnpilex
4.
dauble(8 kytes) Compiler
compile
doesnot
Knouo therm
Knaus
them
deatved and uSer dened ae mon-pomil Eve!
o n ttype dkdata
0 n N yo n c e e

100 ingo value

to20 ao Same type


Cant) datd mlttple
tmes
A' 100 ae'
structure
diffevent
pes
Domive data camot ke divided furthey.
Asray deaived data

colaclton d
Samel stnilao type d data,
aCcessible utth ome
Vaialte.
memou allocated tn
Seguence (Conliguous
ManES
index egns from O
advantages fast access easy to epre Sent
dtsaduan tages s i z e ts fced
tnserltOn&
celeltom are Slod
memoy mtght 90 wasted)
data
Avay collection dhomog@neoue eements
Stpred tn memo Conltguouy

LLLI
2 3 t5 6t
4-dtmensleal)
inta8)
enqth/stze= 8 elenemls

int a[3]; Size- 8X2hyteS


I6 utes
char bC]; Stze= 8XI utes
8 tes
aduntage d ux anay
int xa, 12, 3 4, 15; (5 variabtes)
VS
nt a ; (1viabl)
tnt ats); 1 a m e

eyles 2 t n d e L

address

ivt=2 utes
4 V a l u e

2
34
10 20 30 40 | 50 Aa= address d
000 1002 1004 I06 l008 anyasay
at0 aH At2 At 3 atf

a -D adodress
a me dL an anay-Paddjess
1St elemen
dt0 addhess d. ftst elemenit recdence

= k a [0J = 4(aLol)
= k(%(a10))
a[ = ¥(ati)
TmpaC3j-X(at3) = x(o0013)
Potynter ts
(100.+3 x sxe
d nt)
a vaviable that X(1060 +6)=
holds the
=
*(100¬)
= 40
address kao= at0
kai)= at
a[o= x(ato) =
Value at (ato)
pointe &aL=ati
ts a= address d-
Vajiable aCJ= x(a+i) st elemert
that
holds the a-pointey the
to the irst element d-
aar
a c d d r e s s

apointer to the,5 tntegerS.


uhots. aau d|
defie 100 tnt =100;
Ent a[o; tnt atn;
Jallowed eroy bec0z

Vauabe
tnclude <stdio.h> tnt-4-otes.
tnt main()
nt as]= 12,3,4, 5}
Potrtfop: ka=tpn", a, ka);
paintfaH=: kat1=#Patl, katl);
etusn 0

olp
a =
OXFFE41497 D40 ka =
0XTFFE47497D10
atl= 0X7FFE47497D44 Katl= OXTFFE4747D5t
add OX4-E
add ox4 = 20

intfu', stzed-la)5xtB 20B


20

s t x e

n t aCS)= {10, 20, 30,


n t aL= 10, 20, 30, 59;
40,
40, 50};
Stze-5
Calculated automaticallu,
y Cmpler
int int a[5);
a =10;
a)= 20, Evey declasalion
a2]=30, s-forthe
a3]= 40 Compiler
a[4J 50
tt a[5);
int t
For(t-0; ia=4; t+t)
scanf(.d', %aL]);
t n t at ;
Size missing
9Comptlalion eor (anay
When the Stze is not manltomed m
the brackets;you Ymust have an
tnitialisation."or else uou Uget
Comptlaton emor
int aLJ={10. 20;
Size-2
t n t a[s]-{10, 20,

1oa0o ol
tnttnt]; glooal
Vold moinC)
tnt aC3)=
tnt bCtJ
{10;
0 data
Acqion
1

o stack
2

b stack
012 3

qlobaltnittial value=o

Aocal auto - tnttial value = 4ar


ocal static dnitial value=0

Votd matn()
ent a[3] -{1o,20,30
chos bE3] =fo
b,°c'}J
char c[3= {t,98,99

pant5("ddjd", ac], 2ta), x(at2));


p:20,20,20
Im a - ia) *(att) x (+a)-

pinf Ccd, br21, bD]);


OpC,99
pAntfC"c,1A', °[2J,c[); pC,99
tnt a[i03; D asdaY

4567
tnt t tb[2]B]; 2D anay M
0 2
Repesertalon
Memony Represental+on
b D

Ow1

In C Aanquage we stove bcwby oow.

The aray is stored in a


comltquouS manner
in the memosy inespective d 1D, 2D or 3D
aray

int a2JT= 1o, 20, 30, 40,50, 60


no d) elements
nO-O20uS tneach 0w
(D-d columos
chas aL- A

yte A yte

a 10 20 30
40 50 6o
a gtves the Value at addsess a
XXa
tnt bp133- {o
b 0

o00
t n t cL JLJ={,2,3,4,5,6, 7,8;
o- elements
tn gach xou)=3.

2 3
5 6

tnt []]= { {o,20,30, {40,50.6o


a
1o10 20 30

40 50 60

int a 3 [33;
intj ow by
for(t-o; l<2j it+)
Jor (j=0 j<3, j*+)
Scanf 7.d"', k a[EJLJI);
3
nt aLs];(1-0) assume int-2B

tnt b[33C1): (1-D)


intc[J [33): (-9)
theiy dimension
1D, 2D, 30 anays trresecttve d
the
a e stoyed tn a Contiguous mamey in
mosy. (RAM)
Stosak
1D element buelementF
2D
3D table bu table
nt b[33[ ) ; 2-D YH|Y42 yL3

3 1-D's
9+8
int c 3 J ; 3-D
n o . d - elements

tn e a c h w
92 2-DD's
table
9
t o e no.vouwS
in each table

tobles

ID TII
tnt as]; 1-D
-1-D adra

int alooJ sized tnt 2B


6
=*ajo 1O00 Ven
42
o2..a12
ka[42= ?
a 2 = 1000+ 42 X28
=O00 + 84 =|084 bytes
a- * aLO]= tst element addvess

tnt aE]; 0 2 3

pointf( u', a) 1o 20 30| 40 50


O00 I002 I00t lO0G l008
pkrt(u', kab]);
paintf("u, ka);
OO0
1000
000

paintf( d, a)
:10
a mame darnay 1D
ka addreSs dan aaybase addess
kaoJ-3first element addvess
a [D element Osname in 1D
tnt a[]= {10,20,30
a10 20 30 ut-2B(cay)
000 002 004

xint 5(' sd*, a); pintt d', atl);


patntfd',Aa); pointfd"atl);
/p 1000 002
000 006

aH= &a [oj +1


E l000+(1 x size d
1000 + Ix2= I00 2

katl = 1000 +I
1000 +1x Slze d a50a
= 1000 + IX6= 006

atl

Int aLs); auto,then qldor


Tbage
Values extst!
i globalu, 0
values
Static etst
tnt243] =Sol; n t ai]= {10f
tnt ao; eror
tnt tnt a eor

tnt a 2s]; eror


vt aJ= {1,2,3,4, 5
mismatch, evoy
excess elements
n t aL 2] 0, 20
=
tn arYOUy

iot ap]= io,20

char b3)=
{a, \b', 'c'}; ASCIl Value

char bL] ={a,


b, '¢};
char b[3)= fa'}
chaz ao
b3J= {7,18. 91
irmecit /tyrconversiom/coersion

chas b[3]= a,b,cf error


chos b[3]=
S97','q8', 99' enor
char b[3] =
{,2' no eror
97 not a choaracter
0 choacter
1 character
Tew ie chayacter
t hovizantal tab character
Lnt main)
char a '5+5
pointf(d", a) ;
Yetun 0

58
Mostly we use Xoops to access the elements

int mainC)

tnt a 0 1000
a[aj= 10, 20, 30 20 1002
tt 30 1004
X= atl; l 1=(b»t) (a+);
paintf (d', <);
etun o0;
Ln Some
Comptlers,
Op-1002 X=a+I
is an error.
type msmatch
iTt maln)
tnt a3) {10, 20, 30 =

tnt
= Xat1

y= X(a+1)
pint (d 1.d, x,g);
vetum 0;
3
p: I 20

name d avay
0
+O

kao] addves d
Selement
&a addsess d
asay
a[o name 0
element 0
first elenert's Value
000 1002 (004 1006 1008,
a 00 2o3o40|50 Se d
2 3 4 Lut "2Bsa
a = j000
ka =1000
at0 = 1000
kat0= lo00
t l = IO02
t2 l004 katl= 1000+ Size daxsay
=000 + 10 =
\OlO
ati= &a[oj +i (stze d element)
000 x2 1000 t2i=kal[U
kati= ka[o] +i(Stze d
anay)
1000 + ix10=1000+10t
a[o+i
= 10+

2-D arsay o t o2

ot0to3

int apsjt1 Clanguag


0 23 RAM foll
follows yow majox
0002 order
a2 3 5 002 3 y o u store
10 I1516 004| 5
Values
1415 13+ lO06L
008 1 0
in the system.
10l0
Theorlical 1012 5
epvesentalton 6
1S
tot 28 18
Memoy'Representalion

T
a+o a 1002
a ] E
atl 1o08
a t i kaEj
at2
1022)
a
noame O aDYay.2 a addvess d
dre ss
asay
OW 2
at2- kao+Ioorw
ow3 tncremei
aLOJ Mame d-st douo addyess oo
a= 1000

ati= 000+ (sire d xou)


000+ H+x2) = 10008
at2 lo00
+2(Size d JOUD
=

10004-2(+X2) = I016
xa = \000
ati= 1000 + i(Stze o0u)
&at2 = 1000 +-2(Size d asay
10002(24)
= 048

ati= 1000 +Size d- aay)

at0 addess dst>ouo


kao] addiess d att a t
vou
a[oJ[o1- address d selement
ka address d whole
anas

o[LJJ a[oJ OJ =

mxn tx+) stze delement


no d_ elaments tn
Cach DUU
aro name dL t You

acoJ+0
addresS d element in t oud

the avay
at0 k ato)
atoj+0 = k a[ojtOJ

a ame thethe anay


atO addvess d the ist element tn the
aray

&a0 addre ss d the, 1steenent tn the amay


hole
ka addsess d
a Value the 1st element tn the aTay
aij= x (ati)
a j = ati th the
att address d- the (it)"element in
asay
2D
a mame ô the asnay
at0 address d the st row tn the anay
karo add ress d the ist row in the anay
a address d the whole anay.
atoOaddresses te 1stelement
in the iSt Ow 0the aay
a +0=&a[o)
ato)+0 =&a[oJCo
L a [ o = name dthe 1row in the aray
a[Oj+0= address d the 1Stelement
tn the ow in the aYS AY
tnt matnl)
nt al23o)= {\,2,3,+
paintS(" u", a) 000
paints ("o,u", za); 2 I002

itf(n,u' ka[oi) 3
4
004
006
paintf mf u", ka[0J OJ)
Point(lnu", at):
pointf("nfu', kat\); o 2
poivtf ("mtu', kagj+1)
paint ("ofu', ka[o3+);
Points ("u', apol); a [oJ= X(ato)
PaintF("n u', a[oJ+1);
aint f(ou, aoJo i) ;
pantf ("', aoico)+1);
3
1000 aH &atiJ
000
O00 apjtl=apJ
O00 a[oj+0= kapIoe
O04
008 aool x(aoj+0)
004
IO02
000
X(atjoJ
002 %(1000)
2
1D epre sent a lion

(-L+) elements

size d the arnay = (u-l+1) eleryents

karj= å a[l] +(i-)(stze d elerent)


uhen-0
a]=*aD]+ ixsiacelenet
2D sepveSentali)

TncRou Majd der


int a[3] +); ka

a means ooktng
or the address

at0 Samee ati- a ] |


asol)
a in 1D called addiess 6
element
in 2D called address
Lin 3D Called address table.
addve ss 6
nt asLA);
a JO00 1002 1004 1006
+0 ao1abuiablelato
008 10l0 1012 J0lt
at la10aDlaEkDB
JolG 1018 1020 1022
Ot2 abIbobJaaID)

= name dthe 1st yeu D


the aauy
ato +0 =
addess d thest element
m the St co the aay
aJ= mame the agow tn
the a a
aj+0= address the St element
m the nd vow d
Aa[0 the asaY
a= 000
atO= 100D
ati = kat]
a = |00
ka[o] = 100D
afoJCo) = 1O00
a address

a[o= loDO Yow0

TLameSAelalio addvesses
a nae d 20=ka0]- addess d
dtw 0

ka0 o) addvess d
s element
in the 1St yo
dthe aay
a= IO0o +1= 008
atoj=io00 ato]+1= 002
atojtoj= 20 aotoj+1 = 21

a address d the whote amau


ka[o1 address d3ou 0 in the avay
&a[oo address o eleent o in
the oth dw the anvay
a+I= 1024
ka[o tl = 008
a[o[o) +I =1002

int a rE m= 100p
= 200
)Constant
COnstant
gen a[oJ[o] = I000

i-
attj) =? a
Size nt 2 butes
=

m-
we have to
alculate the no d
elements lotfore act in
the main Ymemdy

kaCtj)= kaD)D) + (+j)x{S


Clement
aL33s) = O00 +(3X200+-5) X 2

O00 + 1210=2210
4-,+1 'u-H

atj)= *a[kJ[LJ
Rouo Major (-4)1,+
3D epveSentaltovl eement
4 elements tn mch 1D
int arjJLA; Oeach
S in each table
2
3 tables 2 4-D S in each
2D

32-D's

3
apJ[Oy
apDY

ooL ap
aLoj aL3

aaddvess 4 the whole anas (3-)


a[o] name d table o
cdleclion d2's ts 3D
int a LoOJ; int a [5ojiooJ[200;
irntaiooJp0J;
4gddress a addess d aaddress
2D arsa 3D aay_

DaL9mame at3L name ar- name


element
table i

aEJ LiJ mame aJJname


Mot delemeRrt -vow
pos Stble
in table

elemert
table
oLO elemert

/name d)
table elernent k
Cw

tablei
elemen

i n t aLpLJ); whexe P ,Y
dmensioyms
(Sizes)
we ae ing acce SS
a
palicular element
ka[ijEjJLKJ
In table tor xow and in that
Dc 9o fvs elernent 'k'.

we have a total d
tables
Befrove th tale, we havea
total o i tables.
n each tale, we have (9,xr)elements
Before table we have a total
xaxr)elements.
table t, efoe theyow we have
j vows
oehoe the th cw d
table i, we have
Also, we will have
xj)elements.
kelemenls ore the a:jL;JLKJ n
the th oW
Total mo d elements
Jede aLJLJ[K)
= + iy+k)

kaL JL3 Lk3


&a[oJ[oJLOJ
T
+.jx + element/

Joehoxe lsehore element


tablei' k
1D n o d e l e m e n t s

in the

int a[b]; D aray

at+
ato atl At2 at3

a mame d the LD aras


at0 address d the oth element
dthe AD anay
ati address d2 the
thelement
Othe AD anay
Note ati= * actj
thethe 0th element
ao)> address
the 4.D aya
katj address d the th element
t h e AD aa

aro) e/value h elemet d


the AD

aL]= name/ va.lue d) h element d


the AD amay
ka address d the whole amay
&at0 =
a[o)+o Stze d whole anay)
ati= aCo) + i(stze d whole avay

ati a[j
att)= * (ati)| the
2)
q i
a m a y
a y

tn
e a c h

t h e 2 )a o Y

delements
n_the
in
d-YoOS

2D o m0

int aL3L+1;
folous
Dw mOjor

tl Order
d-mame d the 2D aay
a +o addyess d the othr0
dthe 2D aray
ati address the th sow
the 2.D aaY-

Note ati= &atij


aCo) address d2 the oth dow
the 2D aay
&aCt) address
dthe tth ouw
the 2D aau
ato name the otn-
in the 2D a a y ow
aro) +0 address d the oth
element i the oth
u
dthe 2p aays
a[o]ti addre ss d the tth
element in the oth
t h e 2D ana

Note: aCo+i= ka[o)tJ


a oo) addvess the oth element
in the ot Dw d the 2D asay

* abti addvess d the ith element


in the 0oth dow dthe 20 a a y

at name d thow
the th oow in

the 2D aoay

at+o rame addsess d the oth

element in the th vow

dthe 2D amay
a)++j addvess d the th eleent
in the thow Othe 20 axay

Note- a)+j =

*atJtjj
aLC) address
in
dthe ith elernent
the ith ow d the 20 aay
ACG= MOmR
OTValue theth element in
the th 3ow dthe 20 aray
a - addaess d the whota
assay
&at+= kato) + i( size
dwhola army
3D

int
ajtJA4J o d
n
elerrents
e a c h voL

mo- tabes dthe


tn the YOwS 3D
asa
30aoaay in
table
the 3 0 a a

ato
a+I

name dthe 3D asay


at0
addyess dthe oth table
the 3D amy
ati-
addsess dthe thtable
thethe 3D aay
Note ati artj
Xa[oj addvess d4 the oth
table d the 3D a vay
kaL) addess dthe ith
table d the 30 aay
aro mame the oth table tn the 3D ava
aLoj+0 addxess the
5 Xow tn the oth table
dthe 3D ava
ato)+i addxess d the tthxow tn the 6t
table d the 30 anay
Note a[o) +-i =
&a[o)tW
kaCoCo)- addess d the othowtn the
oth table
dthe 30 aay
atot) address o the ith ow in
the oth tabe d the 3D aay

ar name dthe tth table in


the 30 aray
at)+0 addres the oth sow in
the tth table d the 30
aay
aL+js addsess o the th oow tn the
tth tabe the 30 aay
Note a[i+j =
ka[iL;)
kat)Co3 address d the &h w tn the
th table
the 3D aay
&a[J addsess d the jth au tn the
tth table d the 30
aa
aELJ mame the thowt
the th table d the 3D aay

LOCo) mame the oth vouw in the


oth table the 3D aray
oth

a[oj Coj +0
oaddvess d the oth
element tn the oth bow
doth table dh
the 3D a a
aLoJ Co)+ addresss d tth
element tn the oth xow
the oth table d
3D aay
attj+ks addvess d the kth elemat
in the th row
the tth table in the
3D a a u

Note acttitk- &a[ilJkj


aLJCk] address dthe kth elemen
tn the th oow d the
th tabte tn the 3D aay
at k) = name/value d the kth
element in the th 3Duo d
th table in the 3D ana
a addvess d the wnole asas
ka+i = &ato) + i(stze d the uhole avay

at-i =& a[t)


motant aCt= * (a+)
at+j = ka[iJLjJ
aticj= * (aci)+j)
acoLp +k= *ai)Lj]ck)
atLjaCk)= * (acitj)+k)

I n DS a[u

&at) = k ato) +(t- ) ( size o6)


element
U2

t
ajLjJ

aAuE u &aLtj)=?
aLIj) -?

I n 4D: t aoj
a+l= aL) address o
kase address
elernent A
& aL)= + 1
(stze o element)
I n 2D int b[i0 203;
atl
& au) addsess d
aLJ= base add +i (stze d Tow)
In 3D: int cEioJ [20)[20];
atl= ka[i) addvess d
table A
a= base addsess +1 (Size
dtable)
Pointers pantey is a Vadable
that holds the addre
ss.
why do we use poiiterS t
C?
because Se it helps
in acce ssig
ointer is a
Vaviable the had waxe.
that centains the (for el:- main
memoy address dh memoy)
anothevaaiable. hadwase has tts address)
Advantage dunamic memoy allocaliom
for Vaables
Oser can create/delete.
Oun time
memoy memoby with the
allocated help dpointers)
dusig
Bun Ume Tunttme
memosy = heap
duamiC Ymembsu
is CYeated
when we dynamically
in an aYea allocate menoy for Vasiabes
memoau eap acts as a memoy pool
Called heap fYom which memo xy
to those Vadi ables
is allacates
Def
otnter ts a vaziable that holds the memoay
address da
vabiable/funclionarsau/structure-
etc
pointer declaxalion
int p all ave cho
Lnt P Same address c character
tntxP data

p ts a potnter vaatable
that potrts to an integer
e
e., ponter to an tnteger
(0)Lteger potnter ov)
pointes that holds&ddress aan data
integer.
P Vaouable
expression ( but mot a vaaiable)
value at p
X deveference operator

3000
int
int -10 P2000

10
2000
Note: ts a
vatble ts a vabiable |
is an inteaeY. .
bu p is pointes to an tnteqer.
or Pis
a pointer
Vasiable that
potnts to an tntege.
Ppxp
int c= 10 int=10
declaralisL-int xP = *x; intp; declaralton
and
ntttaltsalllon
P4 tnihalksalion
both ae, Same

dum)
P= , qives an enor

praintf ("u", p): o 2000

paintf' tp); o 3000


paintf(td, xp); p10
Value at 2000

Lnt Lnt 20
int Xp= X; 20 1o00
2000

pt-2
pointf( d,1A, 1A,1a, 18", <p,kx, kp, Xp);
2 3 , 1000, IO00, 2000, 23

point fd, x); un lme evo


value
a t addresS

23

-*you vefer invalid addresses, ue qet what


tscalled seqmetalion fouilt"(runtme eror)
A

Scam (d", x): xgarbage


addre ss size = 4B, int
int Size- 2B

ntXP
chax K,

poitFa,u, f u', szed(p), sized (),


sizeA ri);
Op 44,t (or) depeds on addess bus
Pintf'nu, u, fu', sizedg(*p)

Op:2,1,4
size(*));

Xp alue at p
int X= 10
int p x10
lodo

2000
int =10
itxp=*;
int X; n nteger data mosL
int Xp imp
Pladdress tegex
data

P is tnteqes pointey
iss pointer to an integes
holds the address d an inteqer vaable
Oy data
Pis a vVaaiable that holds the address
dan integer vasiable data:
bpe dvaaiale p(wch is
deaived
data type)
int x=10
int ¥p= k

Xp=20 420
OUD

p = X(T);| PLoco
2000
¥p = x j

2.0 20
iTt a5- 10,20, 30, 10, 50j
tmt tnt-2B
P-a p= kaLo];
paltf "d,hd, p], a3);
psimtf("a,1d', x(pt), x(a+1));
patot5(4.48d", sLp), ial);
a 10 20 30 40 5
ODO lo02 l0t+ 1006 1008

Pap=katoj\
20 20
20, 20
20, 20
602 PLJ= %(pt)
X(1o00+1)
=(l002)
= 20

pntf (d,1d", pLAJ, at


30,20

ptf(a", pL4));
p:10
arvay VS poimteY pvey example)

PCO PCo pL)

ao) atja) ar:J at) ars) aro) a

P=&a
P

include<stdto.h
tnt main)
lint al5) =
$1o , 20, 30, 40 50j
tntP
P=a; p=*aD];
patntf("hd,d" PEA3, al41)
paintf ("|p hd, }.a", x(pt1), X(at1))
poimtF(" n.d,1d, ALp), 1 ta);
Pt 1;

patntf (" wid,d, d', p[43. a[4), PE-43);


etuan 0;

2 0 , 20, 20
20, 20
20, 20
30, 2.0, 10
iantP tnt
X= 10 (Vs) tnt Xp
P-
XP=10
Pgaobage
ue have )gavbage
By default,
a gasbage value
2 Pgabage
XCgar bage) P lo0o
= 10 ?
Seqmentatiom
fault
If we wuant to
We call this as use pointer
awtld potntes/ bad potnter Vauiable popealy,
uniniliaised potntex put addess tn
the' p'frst
t n t Xp: / p is
lnteger ponter-> vefas,
an
int ;
to
addresSS
|/ x ts an integer
Chas C; / c is a chavacterVaaiableRefers
data'
Vasiable data
to
chax x//q, ts a chavacter pontersefer_ data
to
float f; fts a float vaviable efers to
addre ss
loat x; |l ts floti ng potnter efevsto
a
data
y xefers to
P:2 efers to addre ss address
xc,f tha hold. the data
tnt 2B say)
tnt
sized() sized (tnt) SizedLlo0) =2B
= =

cha c
stze( ) sizedkCchar)
=
Size6()= 4R =

2B

loat f:
Sized Ga) Sizec}float)= Stzei(1sf)= +5
=

double da;
Stzedda)= sizedk (dauble)= stzedL(3)- 8

tnt p slzed To be precise


(1oo);
int 9= sed (13f); Sized-a)
= Stzed (tnt)
int r =sized(3); tn C
int s Stzed-(cha);
poirtf(1d,1d, {d,1*', P9S)
setum 0,

p 2, +, 8,1

Note:- 32 Jt azchttectuve will have


4 byte
pointers whtle a
4+ t azchitecture wil have
8 kyte potnters
chasx float *;
sizeo(p)=+ Slzed()=+ Sized (r)=4+
Stze(xp)=2 slzedx)=1 Stzedg-(xr)=t
Addyess in a system wtl have
a ixed size.
28/48/28 deperds
0n the architecture
Asithmetic Operaltons
address =2B
int 2B

int xp
trt 20;
P= P+
P 2000+I(Size t )
int E
2002 ad
int y-30;
P-4
=
2000-1(size d add
tot
998
PE200D P= p+ whexe
P are pointers
20
2000
add +const
4000 add -cConst
3000

30 add + add x(not allowed)


4000 add- add
add xadd
(not allowed)
x
addadd x (not allowed)
9-P (-P(2e d- element)
(add-add)
opo 1002 1Q O10

P (1010 -1000) 5
2

-P 10l0 1000 ==10 (onine C compiler)

PP+I
P=P- allowed in potnters
=p-9 Johere P,9, ae potnters|

P.9, should
be d
Same type
tnt = 20;
add-2B
i1Tt=22-B
int x
tnt - 30
P=& *y;
psint5(d,d, u,fu', cy,p.4) pt-1,
peint S"o.d, 1d, fu, fu', ,9.p.9)
20 30, 2000, 4O00
20 30, 2002,
3198

it aL5=
2,3, 4, 5j, int 2B
int
Pa[5);
nt a[0];

PairtF(Aa. p-q.-P);
p:10, -10
int *p
int= 20 add= 2B
intx Lnt= 2B
int y= 30

int a,b;
a= p-9
b= -P
point5( 4A,4a , 7a, . a , )
p1000, 1002 1,

ist %9, int= 2B


int aps)={, 2,3, 4,5 O00 l002
1O08
imt b, C
a 1010
P-&aoj;
ka[s3;
C-P-
b 9-P
pavtf(4-414,14', p.q,c,b);
1000, 1010, -5, 5

it a[sj= {1,2,3, 4+,5


int abj=1,2,3,+51;
tnt p-&ao;
int = a [5];
P &aLo)
9aL5]3 |paivntf (1a,d', p-
pmitf(".1d,
oetum 0
p9.01)» -20, 20 >
-i0, 10
-5,5
int 2B
int aLs]= I, 2,3,4,5
it p=kato):
int 9=koa[5];
poinbCu.u,fu, fu', P.9.pt1,9t)
lp: 1000, 1010, IOOI, 101

intP tnt =10 P=k


char xq,
Chax c='A", 9*C;
Float Xr loat f- 102f;
Priit
T=kf;
P+l= 1000 +1 P 2.0|10
=
ToTo+ 1(Size d int) OO0 2000
=i002
ti= 4000 +I(Size d chas 3000
A
4000
=4-001
6000
10-2
+=GOOD +Slze Sloat) 5000 6000
= G004-

P+3 20o0 +3(size dint)


2006

7
>xp+t- X(p++) =
)Xp:
p-p+1;
t tT2B

int =0 10
add=2B
I000
P- ; 1000
2-000

point f(d, }d,1d, xp, xp);


:10, 1002garbage
i n t X=10 ;
int y= 20
tnt xp= ;
Xp
J-Xp+t PPt
paintf(d,jd, 1d, fu", y,z, *p, p)
Op:10,10, gaabae 002

i n t ar5] =
{10, 20, 30, 40, 50f 10O002030 40|50
tnt p- a[0] I008
nt 9-kaL4];

paint(u,fu,.d, jd, p+l, 9ti PPL)


Op: 1000 1O0, 8, -8
tnt a[5] {I0, 2.0, 30, 40, 50
=

nt xp= a[o];
itX'= a[4i
potnt (" u, .u,d, 7d, PtH, t1,P. P-
Dp:1002,1010 4-4
int = 10;
int xp =
k*; 10O00
++P
PL1000
2000

P 1000

+tp =
++(P) E xp= Xp+I
p
int = 10,
int Xp= k ;

Cp)++ 10
000 K

P1000
(Kp)++ E
XP
2000

ti) xp= Xp+1;

P 1000
Kp1

+4++(Xp)= xp= *p+1


p
int = 10; int= 2 B
i t y y=20; add 2B Sa
int Xp= &1;
o10
1O00

y20
(P-); 2.000

PIO0O
-*p; 3000
tii) (¥p)-;
iv -Cp)Cxp);
*p-- int and address
vi) -*p; eednot be
vit p)-- OSame size

vit--(xp);
2
*Cp- -sp)
=*p P=P-1 xp p-1
iy=)-- tv
(xp);
Xp=Xp-1; Epp-1;
p
tt = 10;
ntXp-&

As Xong,
P
as p points to ' ;
C - X-I and Xp= Xp-
a ve both Same

y --P
-p)
P=P-1

Pooblems with Potnters


1int xp; 2 default, we
Xp=100 wll be
havinq a

gahod! Value inp'.


Vow, we ave Say
xCgabag) = 100
whtch ts absuxcd
This will lead to oun-time evror.
Lde call this pointey as wLO Uminitialised
0o) bad pointey.
Soluton
ivntXp (nt x) 100;
=
tnt Xp
data
addyess p-tnt x)o0;
int
addye ss dh an ivrteger
Xp= 100; (type miSmatch)
Seluion2
int y=10
int Xp

Xp= 100; t
xp =(ot *)o,
int xp =NULL it *p;
Xp= 100; lgladal
NOLL xepresemts zero. PlO
#defre NOLL O % =100
We ave
o ut y t o yg to wite, to the
Aeseteence a.

NOLLpointer, we
oth addvess d
the main mem0by
pO th address
oth

dthe RAM/main
accessible toRAM/moain memo
the user menos
.get un[Ume

is ot

Ihts paoblem ts called null potter devefevencing.


Solulion
4 (pl NOLL)
p=10
E xampXe:

tnt malnC)
Lnt pbr-= NULLint Xp=(nt
x)o;
fptr!= NULL
Prtf (Value: pd]n', ?ptr);
else

ptint ("No Valid. pointer

oetum 0;

when address L
an ts not assiqned
to a
pointer, (t has a antom addsessS
which can lead to
misusage
So we assign NjLL to the
So,
pointer to besafe fom
misuse
main

int=0
P AX;
i s destvoy.ed here
Xp 20;
Tuing to update Value
at valic addre ss.
is ts calle
as daqling potnter

dereterence
Danalinq poinleroccurs at the time d
the object destuel+on
when
deleted OY de-allocated. hom
the object ts
modt fytng the Value d the memo withouJt
sttl points to the
sell potnter. he pointer
deallocated memoy memoy locatiom d the

You might also like