Excercise 5-9 (K&R II)

Discussion in 'C Programming' started by mdh, Apr 10, 2007.

  1. mdh

    mdh Guest

    Could someone help clear up some confusion for me.
    I am trying to understand how pointers and multidimensional arrays
    inter-relate.

    Given:

    static char daytab[2][13] = {
    {0,31,28,31,30,31,30,31,31,30,31,30,31},
    {0,31,29,31,30,31,30,31,31,30,31,30,31}
    };


    and char *p;

    if p is set to point to the first row, ( p=daytab[0]) then my
    understanding is that p++ will point to daytab[0][1] etc. What I am
    uncertain about is what happens once p reaches the end of the row?
    Does it then pick up the second row and do the same( ie point at each
    column in row 2)?

    And lets say that one wishes to point to the "ith" row in a 20
    dimensional array. How does one do this.

    I am sure the answers will spark more questions, but thank you in
    advance.
    mdh, Apr 10, 2007
    #1
    1. Advertising

  2. mdh said:

    > if p is set to point to the first row, ( p=daytab[0]) then my
    > understanding is that p++ will point to daytab[0][1] etc. What I am
    > uncertain about is what happens once p reaches the end of the row?


    You stop incrementing it, if you have any sense.

    > Does it then pick up the second row and do the same( ie point at each
    > column in row 2)?


    If that's what you want, do this:

    p = daytab[1];

    > And lets say that one wishes to point to the "ith" row in a 20
    > dimensional array. How does one do this.


    Define what you mean by 'the "ith" row in a 20 dimensional array', since
    it isn't very clear.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Apr 10, 2007
    #2
    1. Advertising

  3. mdh

    mdh Guest

    On Apr 10, 9:51 am, Richard Heathfield wrote:
    >

    mdh said:
    >
    > > Does it then pick up the second row and do the same( ie point at each
    > > column in row 2)?

    >
    > If that's what you want, do this:
    >
    > p = daytab[1];



    Maybe I can clear up my confusion by asking this ( And please
    remember, I am not as well versed in C as you are). Incrementing the
    pointer under these circumstances advances down pointer to the next
    "column" not "row". I am trying to get a conceptual image of why this
    occurs. It is obviously significant that one needs to establish the
    pointer to a particular row initially, and only then can one increment
    it's value to obtain the results(however one wishes to use them) for
    that row.


    >
    > > And lets say that one wishes to point to the "ith" row in a 20
    > > dimensional array. How does one do this.

    >
    > Define what you mean by 'the "ith" row in a 20 dimensional array', since
    > it isn't very clear.


    Say the 15th row in an array arr[20][20].

    Not quite sure if I am articulating this well.
    mdh, Apr 10, 2007
    #3
  4. mdh said:

    > Maybe I can clear up my confusion by asking this ( And please
    > remember, I am not as well versed in C as you are). Incrementing the
    > pointer under these circumstances advances down pointer to the next
    > "column" not "row".


    Here's a two-dimensional array, 5 x 2 elements in size:

    int N[2][5] =
    {
    { 6, 17, 42, 39, 22 },
    { 37, 26, 90, 21, 14 }
    };

    Here's a pointer:

    int *p;

    You can now do this:

    int i = 0;
    p = N[0];
    while(i++ < 5)
    {
    printf(" [%d]", *p++);
    }

    This will print: [6] [17] [42] [39] [22]

    p now points "one beyond" the N[0] array. It's (just) legal for it to
    point there, primarily to make this kind of looping convenient to you,
    but it's not legal to dereference it or increment it further.

    To get to the second row of the array, you have to do this:

    p = N[1];

    > I am trying to get a conceptual image of why this
    > occurs. It is obviously significant that one needs to establish the
    > pointer to a particular row initially, and only then can one increment
    > it's value to obtain the results(however one wishes to use them) for
    > that row.


    Right. What, specifically, is your difficulty with this technique?

    >> > And lets say that one wishes to point to the "ith" row in a 20
    >> > dimensional array. How does one do this.

    >>
    >> Define what you mean by 'the "ith" row in a 20 dimensional array',
    >> since it isn't very clear.

    >
    > Say the 15th row in an array arr[20][20].


    Well, that's a two-dimensional array, 20 x 20, not a 20-dimensional
    array. Using your example, though, you can legally do this:

    p = arr;

    provided that i is in the range 0 to 19. You can now move p along arr
    as before. When you get to the end of the row, ++i and then go round
    again, starting with p = arr again of course.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Apr 10, 2007
    #4
  5. mdh

    mdh Guest

    On Apr 10, 10:26 am, Richard Heathfield>
    > Here's a two-dimensional array, 5 x 2 elements in size:
    >
    > int N[2][5] =
    > {
    > { 6, 17, 42, 39, 22 },
    > { 37, 26, 90, 21, 14 }
    >
    > };
    >
    > Here's a pointer:
    >
    > int *p;
    >
    > You can now do this:
    >
    > int i = 0;
    > p = N[0];
    > while(i++ < 5)
    > {
    > printf(" [%d]", *p++);
    >
    > }
    >
    > This will print: [6] [17] [42] [39] [22]
    >


    Ok...understood.


    >
    > To get to the second row of the array, you have to do this:
    >
    > p = N[1];



    >
    > Right. What, specifically, is your difficulty with this technique?




    >From what you are saying, is this then true? In a 2 dim array, the

    rows seem to be related by the notation "arr[desiredRow]". Advancing a
    pointer moves one along a particular row, from column to column ( or
    element to element) as it were. So, is it true to imagine then that
    each row is an individual array ( it certainly seems to act that way
    with pointer arithmetic). Extending this, it seems to me that the only
    way the rows are related to each other is by the notation described
    above...ie the rows could be in completely different parts of memory?
    As a last extension of this, is C just keeping an array of pointers
    which we access and see as the "row" part of the 2 dimensional array?



    >
    > Well, that's a two-dimensional array, 20 x 20, not a 20-dimensional


    too much coffee!!!!
    mdh, Apr 10, 2007
    #5
  6. mdh said:

    <snip>

    > From what you are saying, is this then true? In a 2 dim array, the
    > rows seem to be related by the notation "arr[desiredRow]". Advancing a
    > pointer moves one along a particular row, from column to column ( or
    > element to element) as it were. So, is it true to imagine then that
    > each row is an individual array ( it certainly seems to act that way
    > with pointer arithmetic).


    Correct. In C, a multi-dimensional array is an array of arrays.

    > Extending this, it seems to me that the only
    > way the rows are related to each other is by the notation described
    > above...ie the rows could be in completely different parts of memory?


    Well, actually they can't (because arrays must be contiguous), but it's
    best to pretend they could be. :)

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Apr 10, 2007
    #6
  7. mdh

    mdh Guest

    On Apr 10, 10:54 am, Richard Heathfield <> wrote:
    >

    In C, a multi-dimensional array is an array of arrays.
    >
    > > Extending this, it seems to me that the only
    > > way the rows are related to each other is by the notation described
    > > above...ie the rows could be in completely different parts of memory?

    >
    > Well, actually they can't (because arrays must be contiguous)


    Richard, thank you. I think I understand the notation much better.I
    also understand a lot better it is what my confusion was about.
    I guess I still do not quite understand why, if a multi-dimensional
    array is an array of arrays, one could also not then "parse" this with
    2 pointers.
    One would do the equivalent of the notation arr[desiredRow], and then
    the other one would do what you have described above. I am sure there
    is a good reason for this, (other than master confusion), but if I
    really understood this, then I think using what you have told me will
    be more insightful.
    Hopefully, this is not exasperating you too much!!!! :)
    mdh, Apr 10, 2007
    #7
  8. mdh said:

    > On Apr 10, 10:54 am, Richard Heathfield <> wrote:
    >>

    > In C, a multi-dimensional array is an array of arrays.
    >>
    >> > Extending this, it seems to me that the only
    >> > way the rows are related to each other is by the notation described
    >> > above...ie the rows could be in completely different parts of
    >> > memory?

    >>
    >> Well, actually they can't (because arrays must be contiguous)

    >
    > Richard, thank you. I think I understand the notation much better.I
    > also understand a lot better it is what my confusion was about.
    > I guess I still do not quite understand why, if a multi-dimensional
    > array is an array of arrays, one could also not then "parse" this with
    > 2 pointers.


    Oh, sure you can traverse with two pointers, if you mean what I think
    you mean:

    int foo[M][N] = { whatever };

    int (*p)[N] = foo;

    for(i = 0; i < M; i++)
    {
    int *q = *p;
    for(j = 0; j < N; j++)
    {
    printf(" %d", *q++);
    }
    ++p;
    }

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Apr 10, 2007
    #8
  9. mdh

    mdh Guest

    On Apr 10, 1:00 pm, Richard Heathfield <>>

    Oh, sure you can traverse with two pointers, if you mean what I think
    > you mean:
    >
    > int foo[M][N] = { whatever };
    >
    > int (*p)[N] = foo;
    >
    > for(i = 0; i < M; i++)
    > {
    > int *q = *p;
    > for(j = 0; j < N; j++)
    > {
    > printf(" %d", *q++);
    > }
    > ++p;



    Thanks...that's what I meant. Thank you again for your help.
    mdh, Apr 10, 2007
    #9
  10. mdh

    Chris Torek Guest

    In article <>,
    mdh <> wrote:
    >Could someone help clear up some confusion for me.
    >I am trying to understand how pointers and multidimensional arrays
    >inter-relate.


    See <http://web.torek.net/torek/c/pa.html>.
    --
    In-Real-Life: Chris Torek, Wind River Systems
    Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
    email: forget about it http://web.torek.net/torek/index.html
    Reading email is like searching for food in the garbage, thanks to spammers.
    Chris Torek, Apr 11, 2007
    #10
  11. mdh

    mdh Guest

    On Apr 10, 11:25 pm, Chris Torek <> wrote:

    > See <http://web.torek.net/torek/c/pa.html>.



    thanks...nice explanation .
    mdh, Apr 11, 2007
    #11
    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. Chad
    Replies:
    27
    Views:
    625
Loading...

Share This Page