section 6.4: Pointers to Structures

The bulk of this section illustrates how to rewrite the binsearch function (which we've already been glossing over) in terms of pointers instead of arrays (an exercise which we've been downplaying). There are a few important points towards the end of the section, however.

page 138

When we began talking about pointers and arrays, we said that it was important never to access outside of the defined and allocated bounds of an array, either with an out-of-range index or an out-of-bounds pointer. There is one exception: you may compute (but not access, or ``dereference'') a pointer to the imaginary element which sits one past the end of an array. Therefore, a common idiom for accessing an array using a pointer looks like

	int a[10];
	int *ip;


for (ip = &a[0]; ip < &a[10]; ip++) ...
or
	int a[10];
	int *endp = &a[10];
	int *ip;


for (ip = a; ip < endp; ip++) ...
The element a[10] does not exist (the allocated elements run from [0] to [9]), but we may compute the pointer &a[10], and use it in expressions like ip < &a[10] and endp = &a[10].

Deep sentence:

Don't assume, however, that the size of a structure is the sum of the sizes of its members.
If this isn't the sort of thing you'd be likely to assume, you don't have to remember the reason, which is mildly esoteric (having to do with memory alignment requirements).


Read sequentially: prev next up top

This page by Steve Summit // Copyright 1995, 1996 // mail feedback