5.9. Summary
You have been introduced to arrays, pointers and the storage allocater.
The last of the topics will prove to be more useful in the next chapter,
but the other two are are central to the language.
You cannot use C properly without understanding the use of
pointers. Arrays are simple and unsurprising, except for the fact that
when it's used in an expression, an array name usually converts into
a pointer to its first element; that often takes time to sink in.
The C approach to support for strings often causes raised eyebrows. The
null-terminated array of character model is both powerful and flexible.
The fact that string manipulation is not built in to the language at
first glance seems to rule C out of serious contention for
character-oriented work, yet that is exactly where the language scores
well compared with the alternatives, at least when speed is important.
All the same, it's hard work for the programmer.
Pointer arithmetic is easy and extremely convenient. It's harder for
ex-assembler programmers to learn, because of the tendency to try to
translate it into what they ‘know’ the machine is doing. However,
much harder for people with very low-level experience is the idea of the
non-equivalence of pointers of different types. Try hard to throw away
the idea that pointers contain addresses (in the hardware sense) and it
will repay the effort.
The facility to obtain arbitrary pieces of storage using
malloc and the associated stuff is extremely important. You
might wish to defer it for a while, but don't leave it for too long. An
obvious feature of C programs written by inexperienced users is their
dependence on fixed size arrays. Malloc gives you
considerably more flexibility and is worth the effort to learn about.
The examples of the use of sizeof should help to eliminate
a few common misconceptions about what it does. You may not use it all
that often, but when you do need it, there's no substitute.
|
Printer-friendly version
The C Book
This book is published as a matter of historical interest.
Please read the
copyright and disclaimer information.
GBdirect Ltd provides up-to-date training and consultancy in
C,
Embedded C,
C++
and a wide range of
other subjects based on
open standards if you happen to be interested.
|