Excercise 5-9 (K&R II)

M

mdh

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

Richard Heathfield

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

mdh

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.

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

Richard Heathfield

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

mdh

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!!!!
 
R

Richard Heathfield

mdh said:

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. :)
 
M

mdh

In C, a multi-dimensional array is an array of arrays.
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!!!! :)
 
R

Richard Heathfield

mdh said:
In C, a multi-dimensional array is an array of arrays.

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

mdh

On Apr 10, 1:00 pm, Richard Heathfield <[email protected]>>

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.
 

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

Similar Threads

Exercise 5-9 K&R 16
The question regarding type of pointers 17
K&R 5-1 9
K&R 1-24 15
K&R Exercise 1-21: entab 10
K&R 5-10 11
Questions about K&R (Kernighan and Ritchi) 57
K&R p 130 29

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top