#include <stdio.h>
char uu[5][4];
main()
{
printf("%p %p %p\n",&uu[0][0],&uu[1][0],&uu[0][1]);
}
gives
C:\a>kwik
0040C1F0 0040C1F4 0040C1F1
This suggests the rightmost array subscript iterates most quickly, so
uu is an array of 5 of array of 4 of char.
Is that understanding correct? Sorry for such a trivial question but I
think my understanding hitherto has been wrong.
Yes, your understanding is correct. And, as it turns out, this
wasn't an arbitrary choice; C couldn't have defined it the other
way without some major changes. (<OT>Note that Fortran's rules
are different.</OT>)
The key point to understand here is that C doesn't really have
multidimensional arrays as a distinct feature. Your declaration
char uu[5][4];
simply declares an array of arrays. The behavior, including the
fact that the rightmost subscript iterates most quickly, follows
from that.
Note that the standard does talk about multi-dimensional arrays.
Strictly speaking, this is redundant; eveything the standard says
about multi-dimensional arrays follows directly from the rules for
one-dimensional arrays. But in this case the redundancy is probably
a good thing; C's array rules are subtle enough that extending them
to the multi-dimensional case is not trivial.
For more information, see section 6 (Arrays and pointers) of the
comp.lang.c FAQ, <
http://www.c-faq.com/>.
Finally, some minor quibbles about your code, unrelated to your
question:
"main()" should be "int main(void)". You can get away with various
other forms ("main()", "int main()", etc.), but there's no good
reason not to do it right.
printf's "%p" format expects a value of type void*, not just any
pointer type. Convert the arguments to the expected type with
a cast.
Since main returns an int, it should actually return an int; add
"return 0;". There are various subtle reasons why you can get away
without the return statement in some circumstances, there's no good
reason to leave it out.
Indentation is important; it shows the structure of your code at
a glance. It's not as important for such a small program, but I
suggest you should get into the habit of *always* indenting your
code correctly.
#include <stdio.h>
char uu[5][4];
int main(void)
{
printf("%p %p %p\n",
(void*)&uu[0][0], (void*)&uu[1][0], (void*)&uu[0][1]);
return 0;
}