back to arays

Chris Nicholson-Sauls ibisbasenji at gmail.com
Mon May 15 23:44:04 PDT 2006


Max Samuha wrote:
> On Mon, 15 May 2006 23:08:26 +1000, "Derek Parnell" <derek at psych.ward>
> wrote:
> 
> 
>>void main()
>>{
>>   int[] a;
>>   int[]* b;
>>   int[]* c;
>>
>>   b = &a;
>>   c = &a;
>>   a.length= 40;
>>   a[0] = 17;
>>   writefln("%d %d", c.length, b.length);
>>   writefln("%d %d", *c[0], *b[0]);
>>}
>>
>>-------------------------
>>This displays
>>40 40
>>17 17
>>-------------------------
>>
>>But I still don't know why one would want to do this?
> 
> 
> No, i certainly don't want to do this. I just expected from D's
> dynamic arrays what was described here in a more readable English as
> arrays being reference types. That's clearer now. Thanks
> 

I have made some similar mistakes before, to be sure.  Essentially D's arrays are really 
structures, one of the fields of which being a pointer to the real data.  Sometimes this 
means another array variable will be pointing to the same data, but this leaves you when 
you try to muck with the array.

Although, in some (not all) cases you can get behavior like what you want by using inout 
parameters (or pointers), and in some other cases (still not all) you could probably use 
slice semantics.

-- Chris Nicholson-Sauls



More information about the Digitalmars-d-learn mailing list