Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > About multidimensional array

Reply
Thread Tools

About multidimensional array

 
 
drazet
Guest
Posts: n/a
 
      08-20-2007
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
 
Reply With Quote
 
 
 
 
pete
Guest
Posts: n/a
 
      08-20-2007
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
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      08-20-2007
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

 
Reply With Quote
 
Army1987
Guest
Posts: n/a
 
      08-20-2007
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

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-20-2007
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

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      08-20-2007
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
 
Reply With Quote
 
drazet
Guest
Posts: n/a
 
      08-21-2007
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?
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      08-21-2007
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
 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      08-21-2007
In article <fabnkp$goe$> drazet <> wrote:
[given]
>int w[2][3],(*pw)[3];
>pw=w;
>
>is *(pw+1)[2] wrong? why?


Yes. As for why, well, one moment:

>and *(w[0]+2),pw[0][0] and *(pw[1]+2) mean?


I think you are making an elementary mistake in parsing these
expressions.

Whenever you go to figure out an expression, you have to know
(or look up) the way the various operators involve "bind". Most
people do this with an "operator precedence table", which is
convenient, but somewhat misleading.

A precedence table tells you things like "multiply before add",
for instance, so that in an expression like:

3 + 4 * 5

you know to multiply 4 by 5 first, then add 3 (result = 23), rather
than add 3 and 4 first, then multiply by 5 (result = 35).

Precedence is misleading in two ways. First -- and most important
-- it tends to make people do *everything* in "precedence order":

int f(void), g(void), h(void);
int answer;

answer = f() + g() * h();

If you, as a human, go to "do the multiply first", you will probably
also call g() first, then h(), then f(), in that order. A C compiler
does not have to do this: it can call f() first, if it "wants to",
and then call h(), and then g(), if it "feels like it", as long as
it then uses the results of g() and h() to multiply, and the result
of that and f() to add, to compute the final value for "answer". If
you make f(), g(), and h() print their names:

#include <stdio.h>
int f(void) { printf("f called\n"); return 3; }
int g(void) { printf("g called\n"); return 4; }
int h(void) { printf("h called\n"); return 5; }

and use this with the "answer" code fragment, the "f called", "g
called", and "h called" lines can come out in any of the six possible
orders (fgh, fhg, gfh, ghf, hfg, hgf).

The second, more obvious problem is that "precedence" fails to tell
you what to do if multiple operators have the "same precedence":

answer = f2 / g2 / h2;

Does this mean (f2/g2) / h2, or f2 / (g2/h2)? So people who use
"precedence" add "associativity" to break ties.

(The C Standards -- C89 and C99 both -- do not use precedence and
associativity. Instead, they use a "fully factored grammar". From
this grammar, one can derive precedence-and-associativity tables.
Several different tables are possible, so if you find two that
differ, they may both be correct. None of this is important as long
as you, and the compilers, get the correct final binding.)

In any case, let us now go back to the expressions:

>*(pw+1)[2]


The subscript operator binds more tightly than the unary "*" operator
-- that is, *a[b] "means" *(a[b]) and not (*a)[b] -- so this binds
the same as:

*((pw + 1)[2])

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

*( *((pw + 1) + 2))

which "means":

*(*(pw + 3))

which in turn "means":

pw[3][0]

But pw points to &w[0], and w[i] has only two valid subscript values
for "i", namely 0 and 1.

Had you written:

(*(pw + 1))[2]

we could translate this to:

pw[1][2]

which *is* valid.

>*(w[0]+2)


This binds as:

*((w[0]) + 2)

Again, *(a + b) can be translated back to simply a[b], so:

*(w[0] + 2)

is just:

w[0][2]

>pw[0][0]


This one should be obvious.

>*(pw[1]+2)


Again, we just use the equivalency and get:

pw[1][2]

Note that *p, for any pointer p, can be rewritten as p[0] (and
vice versa) -- but because the operators have different binding
strengths, you may have to add parentheses when doing this. In
particular, since [] "binds tighter than" unary * -- or as many
people prefer to think of it, [] is "higher precedence" than unary
"*" -- rewriting p[0] as (*p) can require adding these parentheses,
so that the unary "*" still binds to "p" before some other operator
can bind to it.

If you want to know more about arrays, pointers, and arrays of
arrays in general, see <http://web.torek.net/torek/c/pa.html>.
--
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.
 
Reply With Quote
 
drazet
Guest
Posts: n/a
 
      08-21-2007
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].
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
java multidimensional string array geletine Java 12 05-05-2006 09:14 PM
Binding Multidimensional Array to DataGrid epigram ASP .Net 1 07-15-2005 11:02 PM
multidimensional array as argument Huub Java 6 05-29-2004 07:58 PM
Returning a multidimensional array. Ben Java 8 12-18-2003 08:48 AM
slice of multidimensional array Dave Bazell Perl 2 07-24-2003 11:17 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57