Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > order of array subscripts

Reply
Thread Tools

order of array subscripts

 
 
noway@jose.net
Guest
Posts: n/a
 
      03-25-2008
#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.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-25-2008
* http://www.velocityreviews.com/forums/(E-Mail 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

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      03-25-2008
(E-Mail Removed) writes:
> #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;
}

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      03-25-2008
On Mar 25, 6:00 pm, Keith Thompson <(E-Mail Removed)> wrote:
> (E-Mail Removed) writes:
> > #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/>.

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>
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      03-25-2008
(E-Mail Removed) wrote:
> 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]').

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      03-25-2008
(E-Mail Removed) wrote:
>
> 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[i]? 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[i][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[i] 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.

--
(E-Mail Removed)
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      03-26-2008
(E-Mail Removed) wrote:
> On Mar 25, 6:00 pm, Keith Thompson <(E-Mail Removed)> wrote:
>> (E-Mail Removed) writes:
>>> #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/>.

> 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.
--
Jim Langston
(E-Mail Removed)


 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      03-26-2008
Jim Langston wrote, On 26/03/08 06:59:

<snip>

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


<snip>

Please note that this is cross-posted to comp.lang.c where C++ is not
topical.
--
Flash Gordon
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-26-2008
"Jim Langston" <(E-Mail Removed)> writes:
[...]
> 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;
}

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      03-29-2008
(E-Mail Removed) wrote:

> 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"};

--
pete
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
negative subscripts for 2D array of floats lloyd C Programming 2 11-05-2011 08:15 PM
xsl -- using math in element subscripts? William Krick XML 10 08-26-2006 05:26 PM
Allowing zero-dimensional subscripts spam.noam@gmail.com Python 25 06-10-2006 11:53 PM
VHDL Subscripts Cameron, Charles B. VHDL 2 02-26-2004 03:07 PM
Question about some funky behavior with array subscripts Pete C++ 7 11-14-2003 02:02 PM



Advertisments