views:

673

answers:

2

When you have an array like this:

int foo[3][2][2];

and you make:

int *bar = &foo[0][0][0];

Is this the way it works?

*bar == foo[0][0][0];
*(bar+1) == foo[0][0][1];
*(bar+2) == foo[0][1][0];
*(bar+3) == foo[0][1][1];
*(bar+4) == foo[1][0][0];

I'm not sure and have a bit of code dependent on if that works.

A: 

On most systems, yes.

Stefan Mai
Do you have a counterexample?
ysth
Or if not a counter-example, then something from the standard indicating what the layout rules really are for multi-dimensional arrays.
Steve Jessop
+4  A: 

Yes, that's called row major ordering, and is what C is using.

Roberto Liffredo
note that while principally those comparisons would indeed be true, strictly speaking that would be undefined behavior, since he got the pointer from an element in an array of type int[2]. then adding any number greater 2 to it is undefined behavior (tho, only in theory,will always work in reality).
Johannes Schaub - litb