c programming style
Guy Harris
guy at sun.uucp
Mon Jul 22 07:45:25 AEST 1985
> I submit that equating a globally recognized set of constants (i.e. the
> set of integers) with values that differ depending on whatever program
> variable happens to be nearest to them is guaranteed to be misleading.
What changed is not the values of the integers but the operation implied by
the "+" operator. pointer + N causes the pointer to point to the
object (of the type the pointer's type is derived from) N objects (*NOT* N
machine storage units) further into the array that "pointer" is assumed to
point to a member of. This is a bit subtle, but a lot easier to understand
if you realize that pointers are NOT integers but abstract cookies referring
to objects in memory. Most of the confusion stems from a too low-level
mental model of C; people think of pointers as integers indexing a memory
considered as a sequence of machine storage units instead of as the
aforementioned abstract cookies.
The fact that so many people have trouble with this is either because:
1) The low-level model of memory and pointers is so ingrained into
programmer's consciousnesses that trying to make pointers a more
abstract model is doomed to failure
or
2) C just hasn't been taught correctly - the instruction gives a
lower-level model than is appropriate.
The solution to 1) is to rip out pointer arithmetic, use arrays instead, and
have all C compilers do strength reduction to generate multiplication-free
code. The solution to 2) is to avoid giving the impression that C is a
structured assembly language.
Guy Harris
More information about the Comp.lang.c
mailing list