Say I'm using a standard windows RECT structure...
RECT *prect;
I hope that you initialize this pointer to actually point to some RECT
object.
// I can write
prect->left = 0;
// but I can also write
prect[-1].left = 0;
prect[0].left = 0;
prect[1].left = 0;
and the compiler won't complain...
So, what did I define in the first line, a pointer to ONE structure or
a pointer to an ARRAY of structures?
You defined prect to be a pointer to _a_ RECT object. However, nothing
stops you from initializing it to point to the first (or any, really)
element of a RECT array.
The syntax
prect[index]
is simply defined to be a short-cut for
*(prect+index)
So, you're just doing pointer arithmetics followed by dereferencing.
If your pointer were to point to the first element of an array, you
could access this first element by
*prect
or alternativly by
prect[0]
Accessing the other array elements, however, is much more convenient
with the latter syntax:
prect[42]
instead of
*(prect+42)
Since the elements of an array are stored contiguously, this kind of
pointer arithmetic is exactly what you need to access the other array
elements.
How can I define a pointer to an array?
You can still use a variable of type RECT* to point to the first
element of an array and access the other elements via the subscript
operator. With respect to its type, it's still just a pointer to a
single RECT. Depending on who you talk to "pointer to an array" could
mean just that, a pointer that points to the first element of an
array. To other people "pointer to array" means that the pointee type
is an array type:
int (*p)[5] = ...;
which reads: p is a pointer to an array of 5 ints. So, there is some
potential for confusion there ...
Check out
http://c-faq.com/aryptr/index.html for more details on this
subject. There are a couple of things that you should know including
array-to-pointer decay and the type transformations that are applied
on function parameter types.
I don't know in advance how many RECTs there will be in the array.
Sounds like you're in need for some dynamic buffer implementation that
keeps track of its size and capacity and is able to grow if the
capacity limit is reached. Unfortunately this kind of data structure
is not part of the C standard library. Since I'm a C++ programmer who
can simply #include <vector> I can't give you any good advice on how
to proceed in pure C here. Rolling your own data structures is kind of
a PITA. Maybe you'll find a useful library for that. (GLib?)
I'm asking because I can't make WinDbg (nor MSVC debuffer) to show
me the whole array of RECTs.
That's because the type information of such a pointer doesn't really
tell the debugger whether it points to a single object or a subobject
of an array and how large this array is. You have to do this manually
somehow. A decent debugger should let you evaluate custom expressions
like yourpointer[3] so that it tells you what the 4th element of the
array looks like, for example.