order of array subscripts

N

noway

#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.
 
A

Alf P. Steinbach

* (e-mail address removed):
#include <stdio.h>

char uu[5][4];

main()

Needs to have result type 'int'.

{
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?
Yep.


Sorry for such a trivial question but I
think my understanding hitherto has been wrong.

T'is OK.


Cheers, & hth.,

- Alf
 
K

Keith Thompson

#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;
}
 
V

vippstar

#include <stdio.h>
char uu[5][4];
main()
{
printf("%p %p %p\n",&uu[0][0],&uu[1][0],&uu[0][1]);
}

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/>.
Hm.. now I'm confused too. is T arr[4][5] an array of 4 arrays of 5
Ts? or an array of 5 arrays of 4 Ts?
I believe it's the latter. (so, arr[0] is an array of 4 Ts)
Moreover, in the FAQ in question 6.17 there is an additional link
labeled "Yes, Virginia", but the link leads to a page with no content.
(it is not an external link)
Any idea what that is?
Finally, some minor quibbles about your code, unrelated to your
question:
<snip>
 
A

Andrey Tarasevich

Hm.. now I'm confused too. is T arr[4][5] an array of 4 arrays of 5
Ts? or an array of 5 arrays of 4 Ts?
I believe it's the latter. (so, arr[0] is an array of 4 Ts)

No. It's the former. 'arr[0]' is an array of 5 Ts (type 'T[5]').
 
E

Eric Sosman

Hm.. now I'm confused too. is T arr[4][5] an array of 4 arrays of 5
Ts? or an array of 5 arrays of 4 Ts?

An easy way to sort this out is to ask what you get
when you supply just one index: What is arr? Let's
call it an X for the moment, just as a place-holder. What
can we do with an X? We can append [j], producing arr[j]
to access the i,j'th element of the original. Since [j]
can run from 0 through 4, inclusive, an X must be an array
of five elements. So arr is an array of five elements,
and there are four of them.
I believe it's the latter. (so, arr[0] is an array of 4 Ts)

Maybe my way of sorting it out isn't so easy after all ...
Moreover, in the FAQ in question 6.17 there is an additional link
labeled "Yes, Virginia", but the link leads to a page with no content.
(it is not an external link)
Any idea what that is?

It looks like a place-holder for something Steve never
got around to writing, possibly an account of a machine where
the dubious technique actually did/does fail.
 
J

Jim Langston

#include <stdio.h>
char uu[5][4];
main()
{
printf("%p %p %p\n",&uu[0][0],&uu[1][0],&uu[0][1]);
}

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/>.
Hm.. now I'm confused too. is T arr[4][5] an array of 4 arrays of 5
Ts? or an array of 5 arrays of 4 Ts?
I believe it's the latter. (so, arr[0] is an array of 4 Ts)
Moreover, in the FAQ in question 6.17 there is an additional link
labeled "Yes, Virginia", but the link leads to a page with no content.
(it is not an external link)
Any idea what that is?
Finally, some minor quibbles about your code, unrelated to your
question:
<snip>

Maybe this code will help explain things a little.

#include <iostream>

int main()
{
char Data[3][7] = {"Line 1", "Line 2", "Line 3" };

std::cout << Data[0] << "\n";
std::cout << Data[1] << "\n";
std::cout << Data[2] << "\n";

std::cout << Data[0][0] << " " << Data[0][1] << "\n";
}

The output being:
Line 1
Line 2
Line 3
L i

It is an array of 3 arrays of char. Seeing it in this format should make
it fairly clear what is going on.
 
F

Flash Gordon

Jim Langston wrote, On 26/03/08 06:59:

std::cout << Data[0] << "\n";

<snip>

Please note that this is cross-posted to comp.lang.c where C++ is not
topical.
 
K

Keith Thompson

Jim Langston said:
Maybe this code will help explain things a little.

#include <iostream>

int main()
{
char Data[3][7] = {"Line 1", "Line 2", "Line 3" };

std::cout << Data[0] << "\n";
std::cout << Data[1] << "\n";
std::cout << Data[2] << "\n";

std::cout << Data[0][0] << " " << Data[0][1] << "\n";
}

The output being:
Line 1
Line 2
Line 3
L i

It is an array of 3 arrays of char. Seeing it in this format should make
it fairly clear what is going on.

This thread is cross-posted to comp.lang.c and alt.comp.lang.learn.c-c++.
A C++ example is appropriate in the latter, but not in the former.

Here's the C equivalent:

#include <stdio.h>

int main(void)
{
char Data[3][7] = {"Line 1", "Line 2", "Line 3" };

printf("%s\n", Data[0]);
printf("%s\n", Data[1]);
printf("%s\n", Data[2]);

printf("%c %c\n", Data[0][0], Data[0][1]);

return 0;
}
 
P

pete

Hm.. now I'm confused too. is T arr[4][5] an array of 4 arrays of 5
Ts? or an array of 5 arrays of 4 Ts?
I believe it's the latter.

Think harder.

char arr[1][5] = {"1234"};

char arr2[][5] = {"1234", "5678"};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top