Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: Once again ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 8 Topic 26097 of 26977
Post > Topic >>

Re: Once again pointer to first element vs pointer to array cast to pointer to element

by Barry Schwarz <schwarzb@[EMAIL PROTECTED] > May 7, 2008 at 10:40 PM

On Tue, 6 May 2008 23:46:56 -0700 (PDT), Szabolcs Borsanyi
<borsanyi@[EMAIL PROTECTED]
> wrote:

>I know that this topic has been discussed a lot, still I'd appreciate
>a clear cut (and correct) answer:
>I pass a multidimensional array to a function, which is defined as
>int f(int a[10][10])
>{
>  int *b=(void*)a;
>  int *c=a[0];
>  /*...*/
>}
>Now the questions come:
>- Is it guaranteed that b and c points to the same object (or is this
>implementation defined)?

It is true that a, a[0], and a[0][0] all start at the same address, to
be called THE ADDRESS below.

In your first assignment, the expression a is converted by the
compiler to the value and type of &a[0].  You then cast this to void*
and then implicitly convert it to an int*.  Since the value (address)
is properly aligned for an int, everything is well defined and you end
with THE ADDRESS in the form of an int*.

In your second assignment, the expression a[0] is converted to the
value and type of &a[0][0] which is already an int* and assigned to an
int* with no conversion needed.  You also end up with THE ADDRESS in
the form of an int*.

So b and c point to the same address and treat that address as the
address of an int.  The answer to your question is yes they do and no
it is not implementation defined but a standard part of the language.

>- Is it always true that a[1][1] refers to the same object as c[11] ?
>(There is no hole)

This is one for the language lawyers.  Does c[11] (or even c[10])
exist from a purist point of view.  There has been plenty of
discussion previously in this group.  As a practical matter, I don't
believe anyone has identified a system where it doesn't work.

>- Does dereferencing b violate any aliasing rule?

Dereferencing b results in an int.  Since b does in fact point to an
int there is no conflict.  See section 6.5-7.


Remove del for email
 




 8 Posts in Topic:
Once again pointer to first element vs pointer to array cast to
Szabolcs Borsanyi <bor  2008-05-06 23:46:56 
Re: Once again pointer to first element vs pointer to array cast
pete <pfiland@[EMAIL P  2008-05-07 07:19:33 
Re: Once again pointer to first element vs pointer to array cast
Barry Schwarz <schwarz  2008-05-07 22:40:39 
Re: Once again pointer to first element vs pointer to array cast
David Thompson <dave.t  2008-05-19 03:59:52 
Re: Once again pointer to first element vs pointer to array cast
Szabolcs Borsanyi <bor  2008-05-23 00:14:42 
Re: Once again pointer to first element vs pointer to array cast
Szabolcs Borsanyi <s.b  2008-05-23 08:59:14 
Re: Once again pointer to first element vs pointer to array cast
Ben Bacarisse <ben.use  2008-05-23 12:03:50 
Re: Once again pointer to first element vs pointer to array cast
Ben Bacarisse <ben.use  2008-05-23 12:06:24 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 3:55:10 CDT 2008.