order of array subscripts

Discussion in 'C Programming' started by noway@jose.net, Mar 25, 2008.

  1. Guest

    #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.
     
    , Mar 25, 2008
    #1
    1. Advertising

  2. * :
    > #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?
     
    Alf P. Steinbach, Mar 25, 2008
    #2
    1. Advertising

  3. 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) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Mar 25, 2008
    #3
  4. Guest

    On Mar 25, 6:00 pm, Keith Thompson <> wrote:
    > 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>
     
    , Mar 25, 2008
    #4
  5. 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
     
    Andrey Tarasevich, Mar 25, 2008
    #5
  6. Eric Sosman Guest

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

    --
     
    Eric Sosman, Mar 25, 2008
    #6
  7. Jim Langston Guest

    wrote:
    > On Mar 25, 6:00 pm, Keith Thompson <> wrote:
    >> 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
     
    Jim Langston, Mar 26, 2008
    #7
  8. Flash Gordon Guest

    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
     
    Flash Gordon, Mar 26, 2008
    #8
  9. "Jim Langston" <> 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) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Mar 26, 2008
    #9
  10. pete Guest

    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
     
    pete, Mar 29, 2008
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cameron, Charles B.

    VHDL Subscripts

    Cameron, Charles B., Feb 26, 2004, in forum: VHDL
    Replies:
    2
    Views:
    652
    Cameron, Charles B.
    Feb 26, 2004
  2. Pete
    Replies:
    7
    Views:
    430
  3. Replies:
    25
    Views:
    713
    George Sakkis
    Jun 11, 2006
  4. William  Krick
    Replies:
    10
    Views:
    908
    Soren Kuula
    Aug 26, 2006
  5. lloyd
    Replies:
    2
    Views:
    376
    lloyd
    Nov 5, 2011
Loading...

Share This Page