explanation of what **arr is (and bear with me, cuz it’s long. i don’t know what you know or don’t know, so i start at the top. forgive my belaboring the point. =):
in C, an array is synonymous with a pointer. and that pointer has a certain type, like int or short or whatever. that just dictates how many bytes are allocated for each element.
the difference between saying, for example
is that with the former the compiler knows how big the array should be, and automatically allocates space for it, as well as the pointer to its base (element 0).
with the latter, the compiler only knows that you need a pointer to type short, and gives you space for the pointer. if you want it to actually refer to something, you must do that yourself.
(pointers have other uses than just arrays, as you probably are aware. but i’m focusing on just the arrays part, because of the nature of the original question.)
by reserving space with malloc for the number of elements we desire. malloc returns a pointer to void, which we then cast to the type of pointer we need. also, we use sizeof(element) * numElements to tell malloc how many bytes to reserve. and so:
myArray = (short *) malloc( sizeof( short ) * numElements);
now, a two-dimensional array is essentially an array of arrays. if an one-dimensional array is one row, and its elements are columns in that row, then a two-dimensional array is a set of those rows.
it is a collection of pointers, each of which point to a single pointer, which points to the base address of the row.
short **arr; // also means *arr
we declare what will become an array of pointers. each of those pointers is in turn an array. so it’s an array of arrays or a pointer to pointers.
and that is the long-winded way to what it is. email me if you’ve got questions.
[getting silly with it:] to make it really tough, try passing that array of arrays by reference to a function. then you need a pointer-to-pointer-to-pointer-to-short. and that can be a pain to deal with, i know.
for the record, c++ makes this somewhat easier to deal with, and more difficult. (i think it hates multi-dimensional arrays. at least, it seems to go out of its way to make them throw compiler errors.)
[This message has been edited by phlake (edited 07-21-2000).]