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


|