section 5.9: Pointers vs. Multi-dimensional Arrays

Actually, some people (and not just newcomers) are sometimes confused about the difference between a one-dimensional array and a single pointer, too; moving to two-dimensional arrays, arrays of pointers, and pointers to pointers only makes things worse. (But don't lose heart: if you pay attention and keep your head screwed on straight, you should be able to keep the differences clearly in mind.)

The adjective ``syntactically'' in the paragraph at the bottom of the page is significant: after saying

	int *b[10];
an immediate reference to b[3][4] would not be completely legal. It wouldn't be a syntax error or anything, but when the compiler tried to fetch the third pointer and then the fourth integer pointed to, it would go off into deep space, because there isn't a third pointer yet and it doesn't point anywhere.

You might want to draw a picture of the data structures that would result ``[a]ssuming that each element of b does point to a twenty-element array,'' and verify that there are ``200 ints set aside, plus ten cells for the pointers.'' (The picture will be similar to the one on the next page.)

Actually, I'm not sure if having rows of different lengths is the only important advantage of using a pointer array. Another is that the size of the arrays (as we'll see later) can be decided at run-time; another is that the pointers make certain manipulations easier (such as the sorting example we worked through in section 5.6).

page 114

Do study the pictures on this page carefully, and make sure you understand the representations of the name and aname arrays and how they differ. (You might want to refer back to the similar discussion of pmessage and amessage on page 104 in section 5.5.)


Read sequentially: prev next up top

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