472,988 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,988 software developers and data experts.

About multidimensional array

hi,all

i have a question about multidimensional array.

int w[2][3],(*pw)[3];
pw=w;

is *(pw+1)[2] wrong?why?
and *(w[0]+2),pw[0][0] and *(pw[1]+2) mean?

Regards,
Drazet
Aug 20 '07 #1
23 1444
drazet wrote:
>
hi,all

i have a question about multidimensional array.

int w[2][3],(*pw)[3];
pw=w;

is *(pw+1)[2] wrong?why?
and *(w[0]+2),pw[0][0] and *(pw[1]+2) mean?
(A[X]) means the exact same thing as (*((A) + (X)))

--
pete
Aug 20 '07 #2
drazet wrote:
>
i have a question about multidimensional array.

int w[2][3],(*pw)[3];
pw=w;

is *(pw+1)[2] wrong?why?
and *(w[0]+2),pw[0][0] and *(pw[1]+2) mean?
Consider the following display:

[1] c:\c\dispbase>explain int w[2][3]
declare w as array 2 of array 3 of int

[1] c:\c\dispbase>explain int (*pw)[3]
declare pw as pointer to array 3 of int

[1] c:\c\dispbase>alias explain
cdecl explain %1&

a pw probably can't hold any w. Use legitimate code first.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Aug 20 '07 #3
On Mon, 20 Aug 2007 14:55:40 -0400, CBFalconer wrote:
drazet wrote:
>>
i have a question about multidimensional array.

int w[2][3],(*pw)[3];
pw=w;

is *(pw+1)[2] wrong?why?
and *(w[0]+2),pw[0][0] and *(pw[1]+2) mean?

Consider the following display:

[1] c:\c\dispbase>explain int w[2][3]
declare w as array 2 of array 3 of int

[1] c:\c\dispbase>explain int (*pw)[3]
declare pw as pointer to array 3 of int

[1] c:\c\dispbase>alias explain
cdecl explain %1&

a pw probably can't hold any w. Use legitimate code first.
w evaluates to &w[0], which is a pointer to array 3 of int. So
what's wrong with pw = w?
--
Army1987 (Replace "NOSPAM" with "email")
No-one ever won a game by resigning. -- S. Tartakower

Aug 20 '07 #4
Army1987 wrote:
On Mon, 20 Aug 2007 14:55:40 -0400, CBFalconer wrote:
.... snip ...
>
>a pw probably can't hold any w. Use legitimate code first.

w evaluates to &w[0], which is a pointer to array 3 of int.
So what's wrong with pw = w?
It should be &w.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 20 '07 #5
CBFalconer wrote:
>
Army1987 wrote:
On Mon, 20 Aug 2007 14:55:40 -0400, CBFalconer wrote:
... snip ...
a pw probably can't hold any w. Use legitimate code first.
w evaluates to &w[0], which is a pointer to array 3 of int.
So what's wrong with pw = w?

It should be &w.
No.
w is implicitly converted to a pointer to
its first element, in this expression: (pw = w)
It's first element, is an array of 3 char.

Please try new.c with your compiler on strictest warning level.

/* BEGIN new.c */

int main(void)
{
int w[2][3], (*pw)[3];

pw = w;
return pw != w;
}

/* END new.c */

--
pete
Aug 20 '07 #6
Army1987 写道:
On Mon, 20 Aug 2007 14:55:40 -0400, CBFalconer wrote:
>drazet wrote:
>>i have a question about multidimensional array.

int w[2][3],(*pw)[3];
pw=w;

is *(pw+1)[2] wrong?why?
and *(w[0]+2),pw[0][0] and *(pw[1]+2) mean?
Consider the following display:

[1] c:\c\dispbase>explain int w[2][3]
declare w as array 2 of array 3 of int

[1] c:\c\dispbase>explain int (*pw)[3]
declare pw as pointer to array 3 of int

[1] c:\c\dispbase>alias explain
cdecl explain %1&

a pw probably can't hold any w. Use legitimate code first.
w evaluates to &w[0], which is a pointer to array 3 of int. So
what's wrong with pw = w?
pw = w is right,my question is that why the use of "*(w[0]+2),pw[0][0]
and *(pw[1]+2) "can get a right value,but "*(pw+1)[2]"gets a wrong?
Aug 21 '07 #7
drazet wrote:
>
Army1987 写道:
On Mon, 20 Aug 2007 14:55:40 -0400, CBFalconer wrote:
drazet wrote:
i have a question about multidimensional array.

int w[2][3],(*pw)[3];
pw=w;

is *(pw+1)[2] wrong?why?
and *(w[0]+2),pw[0][0] and *(pw[1]+2) mean?
Consider the following display:

[1] c:\c\dispbase>explain int w[2][3]
declare w as array 2 of array 3 of int

[1] c:\c\dispbase>explain int (*pw)[3]
declare pw as pointer to array 3 of int

[1] c:\c\dispbase>alias explain
cdecl explain %1&

a pw probably can't hold any w. Use legitimate code first.
w evaluates to &w[0], which is a pointer to array 3 of int. So
what's wrong with pw = w?
pw = w is right,my question is that why the use of "*(w[0]+2),pw[0][0]
and *(pw[1]+2) "can get a right value,but "*(pw+1)[2]"gets a wrong?
It's supposed to be:

(*(pw+1))[2]
/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
int w[2][3] = {1,2,3,4,5,6};
int (*pw)[3];

pw = w;
printf("%d\n", (*(pw+1))[2]);
return pw != w;
}

/* END new.c */

--
pete
Aug 21 '07 #8
pete 写道:
drazet wrote:
>Army1987 å*™é“:
>>On Mon, 20 Aug 2007 14:55:40 -0400, CBFalconer wrote:

drazet wrote:
i have a question about multidimensional array.
>
int w[2][3],(*pw)[3];
pw=w;
>
is *(pw+1)[2] wrong?why?
and *(w[0]+2),pw[0][0] and *(pw[1]+2) mean?
Consider the following display:

[1] c:\c\dispbase>explain int w[2][3]
declare w as array 2 of array 3 of int

[1] c:\c\dispbase>explain int (*pw)[3]
declare pw as pointer to array 3 of int

[1] c:\c\dispbase>alias explain
cdecl explain %1&

a pw probably can't hold any w. Use legitimate code first.
w evaluates to &w[0], which is a pointer to array 3 of int. So
what's wrong with pw = w?
pw = w is right,my question is that why the use of "*(w[0]+2),pw[0][0]
and *(pw[1]+2) "can get a right value,but "*(pw+1)[2]"gets a wrong?

It's supposed to be:

(*(pw+1))[2]
/* BEGIN new.c */

#include <stdio.h>

int main(void)
{
int w[2][3] = {1,2,3,4,5,6};
int (*pw)[3];

pw = w;
printf("%d\n", (*(pw+1))[2]);
return pw != w;
}

/* END new.c */
thanks a lot, i understand it,the (pw+1)[2] is the address of
w[3][0],but (*(pw+1))[2] is w[1][2].
Aug 21 '07 #9
In article <fa**********@news.yaako.com>, drazet <ky******@gmail.comwrote:
>hi,all

i have a question about multidimensional array.

int w[2][3],(*pw)[3];
pw=w;

is *(pw+1)[2] wrong?why?
The [] has higher precedence than the * so this part:

(pw+1)[2]

is evaluated first. It's equivalent to these:

*((pw+1)+2)
*(pw+3)
pw[3]
w[3]

which is out of bounds.

(*(pw+1))[2] would be something different. Maybe it's what you meant.

--
Alan Curry
pa****@world.std.com
Aug 21 '07 #10
>
Then, given the definition that a[b] "means" (*(a) + (b)), this
Will it not be *((a) + (b)) ?

Aug 21 '07 #11
drazet wrote:
>
.... snip about 140 lines ...
thanks a lot ~!~
Again, this answer, which was exactly as quoted, no less, obviously
required quoting all those 140 lines. Why can't people learn to
snip - its really quite easy.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 21 '07 #12
CBFalconer wrote:
>
drazet wrote:
... snip about 140 lines ...
thanks a lot ~!~

Again, this answer, which was exactly as quoted, no less, obviously
required quoting all those 140 lines. Why can't people learn to
snip - its really quite easy.

"Sometimes I do that as a cheap and easy way of saving
the explanation in my email "Sent box"."
--
pete

"Hey, I have a patent on that. Cease and desist immediately, or you
will hear from my lawyers."
--
Chuck F (cbfalconer at maineline dot net)

http://groups.google.com/group/comp....4?dmode=source

--
pete
Aug 21 '07 #13
somenath wrote:
>

Then, given the definition that a[b] "means" (*(a) + (b)), this

Will it not be *((a) + (b)) ?
It is *((a) + (b)).

(a[b]) does not imply which of a or b is the pointer.

--
pete
Aug 21 '07 #14
somenath said:
>
>>
Then, given the definition that a[b] "means" (*(a) + (b)), this

Will it not be *((a) + (b)) ?
Yes.

Congratulations! You caught out Chris Torek in a mistake. That is such a
rare achievement as to deserve a modding up.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 21 '07 #15
pete said:
somenath wrote:
>>
>
Then, given the definition that a[b] "means" (*(a) + (b)), this

Will it not be *((a) + (b)) ?

It is *((a) + (b)).

(a[b]) does not imply which of a or b is the pointer.
He didn't say it did. Read CT's upthread article again - it is indeed
incorrect, and that was a very good spot indeed from somenath.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 21 '07 #16
pete <pf*****@mindspring.comwrites:
CBFalconer wrote:
>>
drazet wrote:
>
... snip about 140 lines ...
thanks a lot ~!~

Again, this answer, which was exactly as quoted, no less, obviously
required quoting all those 140 lines. Why can't people learn to
snip - its really quite easy.


"Sometimes I do that as a cheap and easy way of saving
the explanation in my email "Sent box"."
--
pete

"Hey, I have a patent on that. Cease and desist immediately, or you
will hear from my lawyers."
--
Chuck F (cbfalconer at maineline dot net)

http://groups.google.com/group/comp....4?dmode=source
So, the only contribution from Falconer is a wrong answer and a net
nanny. Good to see things don't change.
Aug 21 '07 #17
[I wrote]
>Then, given the definition that a[b] "means" (*(a) + (b)), this
In article <11*********************@i38g2000prf.googlegroups. com>,
somenath <so*********@gmail.comwrote:
>Will it not be *((a) + (b)) ?
Oops, yes! Sorry, I managed to typo the placement of the "*" (or
miss out on a set of parentheses, although in this case I just
meant to put the "*" first).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.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.
Aug 21 '07 #18
pete wrote:
CBFalconer wrote:
>drazet wrote:

... snip about 140 lines ...
>>thanks a lot ~!~

Again, this answer, which was exactly as quoted, no less, obviously
required quoting all those 140 lines. Why can't people learn to
snip - its really quite easy.

"Sometimes I do that as a cheap and easy way of saving
the explanation in my email "Sent box"."
Then don't. Most readers have a means of copying a response
somewhere. For example, I have an area named 'Infosave', with some
subdirectories. If I need to keep a message I just copy it there.
Takes about 2 1/4 seconds.

You are using Mozilla, which is a descendent of the Netscape I am
using. Investigate the message menu, especially the copy submenu.

drazet is using Thunderbird, and the same suggestion applies.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 21 '07 #19
Richard Heathfield wrote:
somenath said:
>>Then, given the definition that a[b] "means" (*(a) + (b)), this

Will it not be *((a) + (b)) ?

Yes.

Congratulations! You caught out Chris Torek in a mistake. That is
such a rare achievement as to deserve a modding up.
Does Chris Torek hand out checks (or cheques) for $2.56?

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 21 '07 #20
CBFalconer said:
Richard Heathfield wrote:
>somenath said:
>>>Then, given the definition that a[b] "means" (*(a) + (b)), this

Will it not be *((a) + (b)) ?

Yes.

Congratulations! You caught out Chris Torek in a mistake. That is
such a rare achievement as to deserve a modding up.

Does Chris Torek hand out checks (or cheques) for $2.56?
Not as far as I'm aware.

I would not have spotted somenath's correction, were it not for pete's
reply to him, which - unusually for pete - missed the point completely.
That's because somenath uses a gmail account, and by default I ignore
articles coming from such accounts. I have modded up several gmail
users known to be clueful, however, and he has now joined their ranks.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 21 '07 #21
CBFalconer wrote:
>
pete wrote:
CBFalconer wrote:
You are using Mozilla,
which is a descendent of the Netscape I am using.
Is Mozilla 3.04Gold really descended from Mozilla 4.75 ?

From: CBFalconer <cb********@yahoo.com>
X-Mailer: Mozilla 4.75 [en] (Win98; U)

From: pete <pf*****@mindspring.com>
X-Mailer: Mozilla 3.04Gold (WinNT; I)
Investigate the message menu, especially the copy submenu.
I have no copy submenu.

--
pete
Aug 21 '07 #22
pete wrote:
CBFalconer wrote:
>You are using Mozilla,
which is a descendent of the Netscape I am using.

Is Mozilla 3.04Gold really descended from Mozilla 4.75 ?
Woops - somebody is using older software than I am!

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Aug 22 '07 #23
On Mon, 20 Aug 2007 17:43:13 +0800, drazet <ky******@gmail.comwrote:
>hi,all

i have a question about multidimensional array.

int w[2][3],(*pw)[3];
pw=w;
With a few exceptions that don't apply to your examples, an expression
of type array is converted to the address of the first element of the
array with type pointer to element type. So this statement is exactly
the same as pw = &w[0].
>
is *(pw+1)[2] wrong?why?
Since [] has higher precedence than *, this is parsed as
*((pw+1)[2])

(pw+1) evaluates to the address of w[1].

(pw+1)[0] evaluates to the array (pw+1) points to, namely w[1].
(pw+1)[1] would evaluate to the next array beyond w[1] and (pw+1)[2]
would evaluate to the second array beyond w[1]. Unfortunately,
neither of these two arrays exist since w[0] and w[1] are the only two
defined.

If w[3] existed, then *(pw+1)[2] would evaluate to w[3][0].

If your intent was to get the last scalar element of w, then use
either (*(pw+1))[2] or the equivalent and preferable pw[1][2].
>and *(w[0]+2),pw[0][0] and *(pw[1]+2) mean?
By definition, *(w[0]+2) is exactly the same as w[0][2].

Since pw points to w, pw[0][0] is the same as w[0][0].

Applying the previous two rules, you should have no trouble
deciphering *(pw[1]+2).
Remove del for email
Aug 24 '07 #24

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

Similar topics

5
by: Golf Nut | last post by:
I am finding that altering and affecting values in elements in multidimensional arrays is a huge pain in the ass. I cannot seem to find a consistent way to assign values to arrays. Foreach would...
5
by: TLOlczyk | last post by:
I have a brain cramp and I need some help. I have a chunk of code below which demonstrates a problem I have with multidimensional arrays. I want to keep it simple but something specific is...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
1
by: Mark Smith | last post by:
I'm trying to copy data from a 1D array to a 2D array. The obvious thing doesn't work: int twoDee = new int; int oneDee = new int { 1, 2 }; Array.Copy(oneDee, 2, twoDee, 2, 2); This causes a...
2
by: chris | last post by:
Hi there, I created a Multidimensional array of labels Label lblMultiArray = new Label { {Label3, LblThuTotal}, {Label4,LblFriTotal} }; Now I would like to compare the values in the array,...
1
by: Chuy08 | last post by:
If I have a multidimensional array like the following: Array $records =Array 0 = 30 year, 6.0; 1 = 30 year, 6.0; 2 = Pay Option, 1.0; 3 = Pay Option, 1.0; How could I flatten this to...
5
by: LittleCake | last post by:
Hi All, I have a multidimensional array where each sub-array contains just two entries, which indicates a relationship between those two entries. for example the first sub-array: =Array ( =30...
12
by: filippo nanni | last post by:
Hello everybody, my question is this: I have two multidimensional arrays and I have to create a third one (for later use) from comparing these two. Here is my example code: //BEGIN CODE var...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.