Getting Started

(lily) #1

Chapter 8: C Pointers and Arrays


to conf rus love pointers and will go to incredible lengths to
obfuscate their code with them.


Pointers are the reason that many refer to C as a mid-level rather than a high level
program In high level languages the programmer only deals with
data an akes all the decisions about the addresses. In low-level
languag rs, the programmer assigns the addresses to the data. In
C, we are not required to play with addresses, but are allowed to if we want to.
And, dangerous as they are, some things can only be done using pointers. Pointers
things more efficiently and cleverly than would


ointers, as a simple example consider writing a
ith data from a sequence of contiguous locations
memory. Say you have a string: “Say you have a string:” 22 characters
ll character ‘\0’ all contiguous following the first memory
s that we will give the alias of MemStart. We know that ‘S’ is
‘a’, and so on to MemStart + 23
this sequence, we could send
hat they would all be pushed
ff by the function. But we know
ocontrollers, so we would like to us a
Start as the parameter and have
function start looking there and increment sequentially through memory until
we will agree always ends this kind of sequence (like for
nd with ‘\0’). Now instead of using 23
rame e one parameter
mber are two

f it, but unfortunately many
an
kill

have been lumped with the goto statement as a
programs. This is certainly true

use is why C gu

ming language.
d the compiler m
es, like assemble

also allow us to do many
otherwise be the case.


There are many reasons to use p
function that will do something w
in
followed by a nu
location, an addres
located at MemStart and MemStart + 1 stores
which stores ‘\0’. If we want our function to handle
all 23 bytes as parameters to the function, meaning t
on the stack before the function call and pulled o
that we need to go light on the stack in micr
different method. Easy, just send the variable Mem
the
it sees ‘\0’ which
example, a string which is defined to e
pa ters and pushing 23 by es on t the stack we only have to us
and push only two bytes (addresses are ints, which as you may reme
bytes long).


Sounds simple, and it is once you get the hang o
novice programmers use pointers in much the same way a toddler would use
AK-47. To paraphrase the oft-stated defense of guns, ‘pointers don’t
programs, programmers kill programs.’


To quote K&R
arvelo


, p 93: “Pointers
m us way to create impossible-to-understand

Free download pdf