i
Elr
rr
E
lr
ry
tsa
IE
E
ffi
{r,
G6
E-
@
G'
E
-
Y,,^
t
f"ttti
l-.:.
.I'PB
PUBLICATIONS
Table
of
Contents
Aclotowledgments
2.
Pointers
atrd Arrryr
...
Jntrcduc|ion
Io Second Edhiot
IDtroductio[
To Poitrters,,,,,....,..;...............,.......-.......1
The
& and
,
Operctors, 2
Poifier
Explessions,
4
The
Jargon
of
Pointers,
9
char,
int andfloat
Poihters, 9
Passing
Addrcsses to
Functiohs,
12
Fuictions
Retuming
Pointers,
15
Solyed
Problems,
17
Exercise,
30
Pointers and Stings,
142
The const
Qualifier,
143
Retumirrg eonst Yaluef,
146
Two Dirtensional
A|ray ofCharacters, 147
Array ofPoi\rers
to
Strings,
149
Lirnitario4 ofArray
ofPointers to Strings,
I 52
Solved Problems,
154
F,xerclse. 184. t
tv,'
4. PolnterrandStructures ......,..........------------.,.........189
An Array of
gructures,
191
Mole
about Structures,
192
Structure
Poi ters, I94
Offsers ofstructufe Elemehts,
196
Linked Lists, 200
Stacl$ and
Queuet,
220
Doubly Lhked Lisk,
2ii
Solved Prcblens,
243
Etercise,270
5. Pohters rlrd Dats Structurcs
...,......,.,..........,,,,.....,,,...,....279
Mergirrg
dLinked
Lists, 280
t-
llhat
are
Arrays,
38
Passing
Atay Elernents
to a Function.
Poihters
and
Atays, 43
Pqssing
a4
E ttirc Affoy
to a Function,
TheRealThing,5l
Morc
Thqn
One Di ension,
52
Poinlers
arrd Tfuo
Dimetsional
Arrays,
Poi
tetto
sn Atay,
58
Pdssing
2-D
Arroy to a
Function, 59
Three Dimensiondl
Arrays,
62
Passing
3-D
Array to a Functiok,
66
Retuming
Aftayfrorn
Fuictiot
,
69
Relumifig
3-D Aftay
from
a Function,
Array
of Poi ters,
82
Dyuhtic
Memory Allocation,
84
Solved
Problems,
87
Erercise,
130
Pointers
and Stiitrgs
,.....................133
Wat are
Slrings, 1j3
*
Li ked Lists and Polynonials, 285
Sotting
a Lin*ed List, 297
Circalar
Linhed List, 323
Trees, 326
Binary Tre*,
j28
41
50
3.
Stqhdard Library
String Funcrions,
I38
vt
Traversal
d
a Binary Tree, 33 1
Deletionlrom a
Binary Tree, 336
Threaded Binary Tree, 344
Graphs,356
Solved
Problems, i69
Exercise. 389
6.
PointersMisceIlrtry..,,,.....................................................391
File Poi rets, 391
Pointers to Functions, 394
typedef
with
Furrction Potuters,
397
srgc
and srgv
-
Arguments
to
mairt(
),
398
Poin ers afid
Ydriable
Nurnber
of Argunen
s' 399
rcor,lat
and huge
Poinlers,
40i
Which Poi en
to Use?,
408
Physicol
,4ddrcss
to Segne
:qsel 4
I I
The
Darcing
Dolls, 412
Caps
Locked!,
413
How
Much Memory
Do You
Have?,
415
Exercise,4l7
7. Appllcrdorr
of Pohters
'..,....--........................................423
Exploting
the Dith
423
Dictioiary,
434
Manoging
Database, 439
The Keybod
Queue,
459
InJix to Postfa,
462
Evaluation
of Postfa
Exprcssion,
467
Locatw
Duplicote
Filelanes,
469
Eoshing 474
i
Function Calls and Stqck.
480
Solved Probtens,
V85
I
tnrex |
493
Preface
to
Third
Edition
In all
walks
of
life anythiDg
can
be done
better
the next
lime
around.
I realised
this
more
emphatically
when
I completed
lhe
third
edition
oftlds
book.
Ihus,
Ore
so-cJlled
,perfect'
c'hapters
of
the
second
edition
had
to
be redone
for
more
clarity
and
simplicity.
For
quite
some
time
now
I have
beeo getting
requests
fiom
readers
that
the
chapter
on
.pointers
and
Oata
Structures,
be
made
more
elaborate
and
exhaustivg.
I
have
completely
overhaule.d
this chapter.
Now
it also
includes
binary
treis
ani
graphs,
While
reading
the
fust draft
ofthis
edition
I felt
that
something
is
missin-gin
this
book. It was
only when
I
was
ttuougtr *ittr
rtr!
final
draft
I.realisgd
that
the
missing
link
(poinrer)
wa-s
a
chapter
on
applications
of
pointers
in real-world
programming.
After
all,
no
amount
of theory
is
useful
unless
you
cun
poi
it into
practice.
So
the final
draft
became
semifinai
since
I decided
to
add this
chapter
to the
book.
And
now
I am
presenting
you
the
edition
which
I feel
is
complete
in
all
respects,
lili
I
"-
pmying
tlat
I won't
be required
to wrire
aflother
editiol
of this
book,
Any
author
would
testiS/
that
writing
a
new
edition
is
always
more
palD
in
the
neck than
writing
a
fresh
title.
tt
Poihten
ca be
tuade to
work
ifyou
frddle
,a,ith
them
lo
g
e ough.
Ifyou
Jiddle
with
anything
long
enough
you
wilt
uttimately
fiess it
An
Introtuctionto
Qointrs
f f Tbich
featue
of
C do
beginners
find
most difficulr
to
VV
understand?
The
answer is
easy: pointers.
Other
v
V languages
have pointeE
but few
use tbem
so freely, with
such abandon,
as
C does. Aod
why
rct? Ir is
C,s
clever
use of
pointeN
that
mak€s
it the
excellent language
it
is.
The
difficulty
begiDners
have
with
pointers
has
much to do
with
C's
pointe,
terminology
than lhe
actual
concept.
For instance.
when
a C
progmmmer
says that
a cedain variable
is
a
.
pointer..,
what
does
that mean?
It is
hard to
s€€ how a variable
can poinr
to
something,
or
in a certain
direction.
2
Understanding
Pointers
In
C
Chapter
1: An Introduction
to Pointers
3
It is difficult
to
get
a
grip
on
poioters
just
by listening
to
programmer's
jargon,
In our discussion
of C
pointers, therefore,
we will try to avoid
this difliculty by
explaining them
in terms of
simple
programming concepts.
The fi$t thing
vi,e want to do
is
e\plain
the rationale
ofC's
pointer notalion.
We
oan
print
this address
through
1Le
/-
Program
1
*/
main0
{
inti=3;
following program:
The& and
*
Operators
Consider
the declamtion,
This declaration
tells the C oompiler
to:
(a)
Reserve
space in
memory to
hold the integer
value.
(b)
Associate
the
name i
with this memory
location.
(c)
Srore
the
value I ar this
location.
We
may rcpresent
i's looation
irl the memory
by the
following
memory
map:
i-__}
El--*
6485+
Location
Dnme
I-ocation no.
(Ad&ess)
Figure
l.l
We see that
the computer
has selected
memory
Iocation 6485 as
the
place
to store
the value
3. This location
number 6485
is not a
number
to be relied
upon, because
some other
time drc computer
ult
r\,
may ohoose
a different
location
for storing
th€ value 3.
impofiant
point
is, l's
address in
memory is a number.
printf
( "\nAddress
of i= %u^,
prlntf('hvalue
of i
=
%u",
i)
)
l'he
output
ofthe
above program
would
be:
Addrcss
ot i= 6485
Value
of i=
3
&i)
;
inti=3;
l,ook
at the
fi$t
printf(
)
statement
carefully.
The
.&,
opeBtor
[sod in
this statement
is C's 'address
of operator.
The
expression
&i
retums
the
address
ofthe vagi4llglwhich
in
this case
happens
lo be 6485
pointer
operator
avaikble
in
C is
'*'.
called 'value
at
erator.
It retums
the vaiiE
stored
at a
particdlalEddfess.
also
called
an
'indirection'
at
address'
operator
is
l,hlene
carefully
the output
olthe following programj
/'
Program
2
'/
nrain0
I
inti=3;
printf
(
"\nAddress of i
=
%u',
&i)
;
%d-, i)
;
%d",.(&i));
printf
( \nvalue
ofr
=
other
ess
o
e'value
orator.
printi ("\nvalue
of i=
)
Ttre
评论0