implicite deref on array element access? (indexing)

Steven Schveighoffer schveiguy at yahoo.com
Fri Feb 25 07:43:42 PST 2011


On Fri, 25 Feb 2011 10:15:52 -0500, spir <denis.spir at gmail.com> wrote:

> Hello,
>
> I thought it worked, just like implicite deref on (struct, class) member  
> access. But I cannot have it work:
>
>      auto a = [1,2,3];
>      auto pa = &a;
>      writeln((*pa)[2]);      // ok
>      writeln(pa[2]);         // segfault

Because indexing a pointer like ptr[n] is the equivalent of *(ptr + n).

This is how it is in C.

Fun fact, you can avoid array bounds checks (if you know the index is  
valid) by doing arr.ptr[n]

-Steve


More information about the Digitalmars-d-learn mailing list