On Mon, 01 Apr 2013 23:23:49 -0400, Luís Marques <[email protected]>
wrote:
On Tuesday, 2 April 2013 at 02:52:48 UTC, Steven Schveighoffer wrote:
You see, indexing does NOT dereference the pointer, it's an index for
that pointer. c[0] means *(c + 0). A pointer is essentially an
unchecked slice, with undefined length. This is how it works in C also.
c[1] is the same as *(c + 1), completely consistent (and also sets b to
42, 42)
OK, I think I see where I went astray. I was a case of bad induction
from a few tests :-)
So, I guess what is happening is the following, right?
int[2] a;
int[2] *c;
c = &a;
c[0] = 7; // same thing as below
a = 7; // same thing above
(cast(int*) c)[0] = 7; // but different from this
I verified that c is a pointer to a.ptr, I guess what I didn't consider
is that because c points to int[2], the assignment becomes the same as a
= 7, and not a[0] = 7.
Still, what do you think of the struct vs AA automatic pointer
dereferencing?
a pointer defines indexing. To have implicit dereferencing for indexing
on any pointer type would not be good.
For example:
string[int][2] aas;
string[int] *aaptr = aas.ptr;
auto x = aaptr[1];
If x is not a pointer to aas[1], then you would have to access it using
*(aaptr + 1), which would suck. Plus it would make pointers to indexed
types inconsistent with all pointers to types that don't define indexing.
-Steve