473,503 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Excercise 5-9 (K&R II)

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.

Apr 10 '07 #1
10 1472
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.
Apr 10 '07 #2
mdh
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.

Apr 10 '07 #3
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[i];

provided that i is in the range 0 to 19. You can now move p along arr[i]
as before. When you get to the end of the row, ++i and then go round
again, starting with p = arr[i] 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.
Apr 10 '07 #4
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!!!!


Apr 10 '07 #5
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.
Apr 10 '07 #6
mdh
On Apr 10, 10:54 am, Richard Heathfield <r...@see.sig.invalidwrote:
>
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!!!! :-)

Apr 10 '07 #7
mdh said:
On Apr 10, 10:54 am, Richard Heathfield <r...@see.sig.invalidwrote:
>>
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.
Apr 10 '07 #8
mdh
On Apr 10, 1:00 pm, Richard Heathfield <r...@see.sig.invalid>>

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.
Apr 10 '07 #9
In article <11**********************@y5g2000hsa.googlegroups. com>,
mdh <md**@comcast.netwrote:
>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.
Apr 11 '07 #10
mdh
On Apr 10, 11:25 pm, Chris Torek <nos...@torek.netwrote:
See <http://web.torek.net/torek/c/pa.html>.

thanks...nice explanation .

Apr 11 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
8525
by: Collin VanDyck | last post by:
I have a basic understanding of this, so forgive me if I am overly simplistic in my explanation of my problem.. I am trying to get a Java/Xalan transform to pass through a numeric character...
1
11418
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a...
0
2407
by: Thomas Scheffler | last post by:
Hi, I runned in trouble using XALAN for XSL-Transformation. The following snipplet show what I mean: <a href="http://blah.com/?test=test&amp;test2=test2">Test1&amp;</a> <a...
4
3005
by: Luklrc | last post by:
Hi, I'm having to create a querysting with javascript. My problem is that javscript turns the "&" characher into "&amp;" when it gets used as a querystring in the url EG: ...
4
3203
by: johkar | last post by:
When the output method is set to xml, even though I have CDATA around my JavaScript, the operaters of && and < are converted to XML character entities which causes errors in my JavaScript. I know...
8
2781
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"...
11
6381
by: Jeremy | last post by:
How can one stop a browser from converting &amp; to & ? We have a textarea in our system wehre a user can type in some html code and have it saved to the database. When the data is retireved...
14
5907
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
12
10059
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
7
4598
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get...
0
7204
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7342
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.