473,657 Members | 2,449 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 multidimensiona l 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 1488
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.i nvalidwrote:
>
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.i nvalidwrote:
>>
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.i nvalid>>

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************ **********@y5g2 000hsa.googlegr oups.com>,
mdh <md**@comcast.n etwrote:
>Could someone help clear up some confusion for me.
I am trying to understand how pointers and multidimensiona l 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

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

Similar topics

9
8556
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 reference (i.e.  ) and it seems to be converting the character to its UNICODE representation. Take this source XML document: <?xml version="1.0" encoding="UTF-8"?>
1
11435
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 href="mailto:DrTebi@yahoo.com">DrTebi</a> This would work like a regular mailto link in any browser, but wouldn't be visible to spiders if they don't have a function to decode it.
0
2413
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 href="http://blah.com/?test=test&amp;amp;test2=test2">Test2&amp;amp;</a> This results in the following HTML Code:
4
3017
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: /mypage.asp?value1=1&amp;value2=4&amp; ... which of course means nothing to asp.
4
3217
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 that I could externalize my JavaScript, but that will not be practical throughout this application. Is there any way to get around this issue? Xalan processor. Stripped down stylesheet below along with XHTML output. <?xml version='1.0'?>...
8
2802
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);" TextBox2.Attributes.Add("onKeyPress", jscode) You will notice that jscode contains the JavaScript Logical And operator (&&). However, ASP.NET renders this as &amp;&amp; in the code that is
11
6422
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 and
14
5921
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 because of this, so I hope some of you gurus can enlighten me with this :) In what circumstances can the "&amp;" in the source code be involuntary changed to "&" by a browser when or other software, when editing and uploading the file to the web...
12
10093
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) ..... // xsl contents abc.aspx?p1=v1&amp;p2=<xsl:value-of select="$v2" />
7
4606
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 BeautifulSoup to clean those up? There are various parsing options related to "&" handling, but none of them seem to do quite the right thing. If I write the BeautifulSoup parse tree back out with "prettify", the loose "&" is still in there. So...
0
8395
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8310
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8826
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4155
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.