Thread overview | ||||||
---|---|---|---|---|---|---|
|
February 25, 2011 Re: implicite deref on array element access? (indexing) | ||||
---|---|---|---|---|
| ||||
This should explain everything: auto a = [1,2,3]; auto pa = &a; writeln(&pa); writeln(&pa+1); Do the math! |
February 25, 2011 Re: implicite deref on array element access? (indexing) | ||||
---|---|---|---|---|
| ||||
Actually that doesn't explain anything! What I mean is, arrays in D are not the same as arrays in C. auto a = [1, 2, 3]; auto pa = &a; writeln(&a[0]); writeln(&a[1]); writeln(&a[2]); writeln(&pa[0]); writeln(&pa[1]); writeln(&pa[2]); 9A2E40 9A2E44 9A2E48 12FE34 12FE3C 12FE44 Afaik D arrays have a length and then a pointer to the contents of the array (someone correct me on this if I'm wrong?). |
February 25, 2011 Re: implicite deref on array element access? (indexing) | ||||
---|---|---|---|---|
| ||||
P.S. I got bitten by this when I was interfacing with C. Only in my case I used multidimensional arrays. I just assumed they were the same thing as in C, but I was wrong. |
February 26, 2011 Re: implicite deref on array element access? (indexing) | ||||
---|---|---|---|---|
| ||||
On Friday, February 25, 2011 07:15:52 spir 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
>
> Denis
The _only_ time that dereferencing is done automatically in D is with the dot operator.
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation