Book 10 Dec 2024
Book 10 Dec 2024
rvRES O f POINTERS
e m em or y space.
.1 ffft. rs save th . . .
v poiJlte po in te r 1s faster becaus e d ata is man·1 .. ,_
ut ed with the ad.dres
s, i.e. direct
1i} time with
ution . n. Pu
f) t'
ss to m em or y 1oc at io
.~1
, ,_e,ee . rs Th . the m em or y space
w ith th e poin • r assig
e, no ry is accessed efficiently te
• e po m te ns
,n
'fbt als .
.
,wJ and o releases 1t. d
e us ed w ith da ta st ructures. The eful fo r re pr es en tin g two-dimensional an
&Jinters ar rays.
y are us
/ii'! :ulti-diJnensional ar .
ts of an y type of ar ray ir respecti.ve of .1ts subscnpt range.
"re can access elemen
(~} e handling.
. pointers are used for fil
lly.
(11)
are us ed to al lo ca te m em or y dynamica
(rii) pointers
fJ pOINTERS AND A
DDRESS d
co m pu te r memo ry 1s • made b y the serru•-con uctor
ed
icat • The Byte
""r y of . mpl
the po in te r is .no t co s a ca paci ty to sto re a bit either O or 1.
~ tb"" . Each cell ha 9.2. .
:,.iJnOlogY- Memo?' co
m pn se s bi na ry cells em or y addr ess as shown in Figure
ue m
su ch byte has a uniq
1'35' unit of 8 bits. Every
Address
0
I
2
I
I
I
I
I
I
f IGURE <J.l
rved.
ory according to its data type is rese
riable of an y da ta type is declared, mem
. When a va
"tlample,
65550
65551
65552
65553
65554
c\..,( 1(. I\ TI ,\ ;
int *x;
float *f;
char *y;
(i)
-
In the first statement 'x, is an integer pointer and tells the compiler that it hold
8
dress of any integer variable. In the same way, 'f is a float pointer which stores th the ad.
of any float variable and 'y, is a character pointer that stores the address of any \address
variable. • • c aracter
(ii) The indirection operator(*) is also called the dereferencing operator. When a point 18.
erenced, the value at that address stored by the pointer is retrieved. er derer.
(iii) Normal variable provides direct access to their own values, whereas a pointer proVides. .
rect access to the values of the variable whose address it stores. 1.J1d1.
(iv) The indirection operator (*) is used in two distinct. ~ays with pointers, declaration
dereference. . . • ~
(v) "Yhen a pointer is declared, a star(*) indicates that it is a pointer and not a normal Variable.
• (~) When the pointer is dereferenced, the indirection operator indicates that the value at that
memory location stored in the pointer is to be accessed rather than the address itself.
(vii) Also note that * is the same operator that ·can be used as the multiplication operator. 1he
compiler knows which operator to call, based on the context.
(viii) ' & ' is the address operator and represents the address of the variable. %u is used with the
print f ( ) for printing the address of a variable. The address of any variable is a whole nwn-
ber. The operato.r ' & ' immediately preceding the variable returns the address of the variable.
In the below given example ' &' is immediately preceding the variable 'nurn' which pro- ,
vides the address of the variable.· . • • ·
void main()
{
int mnn;
clrscr();
printf("Enter a Number=");
scanf("%d",&num);
printf("Value of num =%d\n",num);
printf("Address of num=%u\n",&num);
getche();
}
OUTPUT:
Enter a Number= 20
Value of num = 20
Address of num = 4066
' '
Coftlputer Programming with C
voi d ma in( )
(
OUTPUT:
En ter Va lue of 'a' & 'b' . ·:
8 4
Va lue of a=8 & b=4
Ad dre ss of a=4056
Ad dre ss of b=4058
Ad dit ion of 'a' & 'b' 12
*pa=4 *pb=4
pa=24350 pb=24350
.
Ad dit ion of *pa & *pb 8
Explanation:
In the above program, addresses of vari
ables 'a, and 'b' are assigned to pointers 'pa'
'pb'. The statement pa= pb; means the valu a:J
e of pointer 'pb' is assigned to pointer'pa',MW.
both the pointers have the same value
and they point to the same variable 'b: Hencc
addition is 8. ,tht
void main ()
{
int k,*x ;
/* clrs cr (); * /
for(k=O;k<=3;k++)
prin tf(" %u" ~x[ k));
}
OUTPUT:
7272 24330 30559 27753
Explanation:
In this program, pointer xis not initialized. The succe
ssive locations are displayed.
(i) The forgetful assignment of a
new memory location in a pointer is called the pointe
lion. This happens when the wild pointer accesses the
location of the Wild point r~lt~.
pointer converts the legal pointer to the wild pointer. er. •1ie "',J
··lij
(ii) Sometimes the pointer attempts to acces
s the data that has no longer life.
ci rs cr () ;
. •;
)C..,40;
• •pm~so; in vk) al id o p er at io n
I it l t f( "k c% d" , ,•
pr
\r01•
d m ai n ()
{
in t x ,* x l;
C ha r Y ,* yl ;
fl oa t z, * zl ;
C lr sc r( );
1Sft't:fll t kPN ti
prin tf("E nter integ er, char acte r, float Valu es \nn) ;
scanf ("%d \c \f",& x,&y ,&z);
xl:::a&x;
yl=&y;
zl=&z;
print f{"A ddre ss of x = %u\n n,xl) ;
print f("A ddre ss of y = \u\n n,yl );
print f{"A ddre ss of z ~ \u\nN ,zl);
xl++ ;
yl++ ;
zl++ ;
prin tf("\ nAft er Incre ment in Poin ters\ nn);
print f{"\n Now Addr ess of x=% u\nn, xl);
print f("N ow Addr ess of y=% u\n", yl);
print f("N ow Addr ess of z=% u\nn ,zl);
prin tf("\ nSiz e of Integ er: %dn ,size of(x ));
prin tf("\ nSiz e of Char acter : %dn ,size of(y ));
prin tf("\ nSiz e of Floa t: %d" ,size of(z) );
}
OUTPUT:
Ente r integ er, char acte r, float Valu es
2 A 2.2
Addr ess of x = 4046
Addr ess of y c 4053
Addr ess of z =·4058
Afte r Incre ment in fain ters
Now Addr ess of x ~ 4048
Now Addr ess of y = 4054
Now Addr ess of z = 4062
Size of Inte ger: 2
Size of Char acter : 1
Size of Floa t: 4
Explanation:
Observe the output 4046 is the address of integer 'x', 4053 is the addre
ss of character Y.Di
4058 is the address of floating point number 'i. On the increment
of the pointer the~
of integer, character and float will be 4048, 4054 and 4062, respectively.
This is because apail-'
is incremented. if points to immediately next location of its type.
~ r •
9.15 Wnte a program to show the effect of increment and decrement
operators used as prefila.iid
, suffix with the pointer variable.
void main ()
{
int i, *ii;
puts ("En ter Valu e of i•");
scan f (''%dH, &i);
Pointers
9-17
.i.,.,,i; ,.
i crO; of i :a %u\n ",ii); J
l • ,, ...
f
,1,s f (''Add ress
of i C
%u\n ",++ ii); l ... \
ptifl~ f ("Add ress
of i = %u\n ",ii+ +);
pt.i.~t f ("Add ress
of i z:r. %u\n ",--i i);
pri f (''Add ress
of i = \u\n ",ii- -);
pri~~ f ( "Add ress ! I
pri f ( "Add ress of i = %u\n ",ii);
t:>
I .- II V
I print '
I
! ~l)'fl'V'f:
' vaiue of i= 8 o- / I
60
I t'fltet
.. ess o f 1. =
! ,'ddr
.
4 0 62
of i ==
40 r-( \
..........
,'ddress of i ==
iddress
4 0 62
O62
rJ -
" of i = 4
,'ddress
,'ddress of
i = 4 0 62
i = 4060'
rJ)
,'ddress Of
tf(" \n The
Poi nter s have the differen t add ress .•);
,r:i.n
p t! ("'\n Add
ress of a(j) •%u ",j) ;
ptj.! l f("\ n Add ress of
a(k )•\u ",k) ;
?tin t
I "11°•
poi nter s hav e the same add ress .
.rl'J'L J•·
ov• • ) .,. 4056
'f'IIO
111e s of a (J
, J.ddte s of a ( k) • 4056 _______________
_...J
dd.r:ess _ _ _ _ _ _ _ _ _ _ _ _
;.
. . . .
£,xplanatio~: th m the above program. The pointer van -
'fhe comparison betw een e two pomt~rs JS done
arison is made. Toe comparison of poin t·
able should _be _of e sam e d~~ typ~ while a comp
th
, in this program pointers , j, and , k
1
pOINTERS AN D ARRAYS
9,9
ts to the address of the first element
(i) Array name by itself is an address or pointer. It poin
together with their addresses can be dis-
(0th element of an array). The elements of the array
are always stored in contiguous mem ory
played by using array name itsel£ Array elements
locations.
. .1
Programs in this regard are explained below.
r9.18 Write a program to display elements of an
l
array. Start element counting fro~ instead of 0.
void main ()
(
OUTPUT:
: fl
2 4 6 8 10 - ,;
Explanation:
element num ber is added in the base
Array element counting always starts from •o~ The
one is subtracted from base address of an
address and each element of an array is accessed. If
By adding one to its redu ced base a d ~
~r~ay, it points to the prior address of 0th elem ent
it 15 possible to start element coun
ting from 'I'.
Pointers 9-37
I 2. int *t,x;t=&x;x=ll;
void main () *t++;
l clrscr();
{
·~ printf("%d",*t);
flo~t x=l0.25,*f;f=x;clrscr();
pr1ntf ("%g", *f);
6.
3.
void main()
void main ()
{
int t[]={l,2,3,4,S};
float x=l0.25,*t,•f; int *p, *q, •r;p=t;q=p[l]i
t=&x;
r=-p (2]; ..
f=&t;
clrscr () ; ,r1;
clrscr(); printf("%d \d %d'',•p, ♦q,
printf("%g",**f);
7.
4.
void main{)
void main ()
{
{
C
1-1'1
t
9.
void :""3i: -. ()
{
ir.t x=S;
ir.t •l:"r -,->.,- v . py;
x - ' .,
...
clrs ,.._.._ , ().
prir .~f( "ld~ ,px );
, .