Chapter 8: C Pointers and Arrays
create pointers that point
iscipline, however, pointers can also be used too buffer of an IBM PC. I made
is, I started a count with 1 instead of 0, and wrote
last data byte to an address that was one byte outside of the video buffer. That
e was only occasionally important enough to crash the system. When your
ittently with no apparent rhyme or reason, you may well
suff d bug to find.tored data) that contain the address of other variables
ters to data, pointers to pointers, pointers to
le and clear as possibleceding its name with an *, called the&, called the address operator:ve two worse choices been made for
mbol d for
f the variable name
could have been written to require that
e suffix follow the pointer everywhere, thus helping us know what we are
ling with. But NOOOO.... * and & were chosen, and confusion reigns eternal.
e could fix this problem by adding a couple of defines to alias the * and & as ptr
addof, and we could require that we always name pointers in such a way that
, but since our goal is to learn C as Ritchie and
NSI intended and not my version of C, we’ll do it the hard way. What? youwhen they are used carelessly, and it is easy to
somewhere unexpected. With d
achieve clarity and simplicity.”
I once used a pointer to sequentially access the vide
a simple ‘fence-post’ error, that
the
byt
computer crashes interm
be ering from a bad pointer use. It can be a damn har
To recap: variables (RAM s
are called pointers. You can have poin
pointers to pointers to... but let’s try to keep it as simp
(whoops... too late).
We declare a variable to be a pointer by pre
indirection or dereferencing operator:
int *q; // q is a pointer to an int
We get the address of a variable using
q = &v; // put the address of v in the pointer q
Never in that annals of mnemonics ha
sy s. Instead of *, the letters ‘ptr’ could have been chosen
in the second use o
for pointer, an
&, the letters ‘addof’. There is no indicator
‘q’ that it is a pointer, but the compiler
som
dea
W
and
we always know it is a pointer
A
don’t think it will be hard to remember what the * and & do? Wait till you run
into things like: