Implicit dereferencing
Ali Çehreli
acehreli at yahoo.com
Mon Apr 1 22:17:30 PDT 2013
On 04/01/2013 05:07 PM, "Luís Marques" <luismarques at gmail.com>" wrote:>
Consider this (non-portable) code:
Perhaps it is obvious to everyone but allow me still: It is not
non-portable code, but its behavior is undefined.
> {
> int[2] a;
> int[2] b;
> int[2] *c;
c is a pointer to a single int[2].
> c = &a;
>
> c[0] = 7;
> assert(a[0] == 7); // OK, as expected
>
> c[1] = 42;
> assert(b[0] == 42); // do we really want this semantics?
A manifestation of undefined behavior.
> But "c[1] = 42" is equivalent to "(*(c+1)) = 42", which might not be
> obvious. In this case it ends writing to b[0].
It seems to be so but it is not defined to work that way.
Ali
More information about the Digitalmars-d
mailing list