"Michael Comperchio" <mcmp...@...> wrote:
> Peter Nilsson wrote:
> > "Michael Comperchio" <mcmprch@> wrote:
> > > ...
> >
> > The declaration...
> >
> > int a[2][3];
> >
> > ...is...
> >
> > |<--------- [0] ---------->|<---------- [1] --------->|
> > +--------+--------+--------+--------+--------+--------+
> > | [0][0] | [0][1] | [0][2] | [1][0] | [1][1] | [1][2] |
> > +--------+--------+--------+--------+--------+--------+
>
> You may find it useful to think this as you work with it,
I do, because it's the way the C language specification
says it must be.
> but in reality there is NO guarantee that this will be so.
6.2.5p20:
"...An array type describes a contiguously allocated nonempty
set of objects with a particular member object type, called
the element type. ..."
> > In today's massive memory spaces this is more likely to be
> > so, but NOT guaranteed.
It's been guaranteed in C since 1989, and I've never known it
not to be the case prior to that.
>From my c89 draft...
"...Except for bit-fields, objects are composed of contiguous
sequences of one or more bytes, ..."
"An array type describes a contiguously allocated set of
objects with a particular member object type, called the
element type. ..."
> > ...not...
> >
> > +-----+-----+
> > | [0] | [1] |
> > +-----+-----+
> > | |
> > | +--------------------+
> > v v
> > +--------+--------+--------+--------+--------+--------+
> > | [0][0] | [0][1] | [0][2] | [1][0] | [1][1] | [1][2] |
> > +--------+--------+--------+--------+--------+--------+
> >
> > ...and certainly not...
> >
> > +-----+-----+
> > | [0] | [1] |
> > +-----+-----+
> > | |
> > | +------+
> > | v
> > | +--------+--------+--------+
> > | | [1][0] | [1][1] | [1][2] |
> > | +--------+--------+--------+
> > v
> > +--------+--------+--------+
> > | [0][0] | [0][1] | [0][2] |
> > +--------+--------+--------+
>
> This is the layout that is, in fact, what multi dimension
> array access is best thought of.
It's interesting that you don't see C as an abstract language,
yet you insist on interpreting arrays of arrays in a purely
abstract, and non-representative, way.
Here's a simple program...
#include <stdio.h>
#include <string.h>
void fill(void *p)
{
int i;
unsigned char *uc = p;
for (i = 0; i < 25; i++)
*uc++ = "abcdefghijklmnopqrstuvwxy"[i];
}
int main(void)
{
int i;
char a[5][5];
char b[25];
fill(a);
fill(b);
for (i = 0; i < 5; i++)
printf("%.5s %.5s\n", a[i], &b[i * 5]);
printf("%.25s\n", &a[0][0]);
printf("%.25s\n", &b[0]);
return 0;
}
There are no hidden pointers embedded in the array called a.
There is no reason to think there are.
--
Peter