back to arays

Derek Parnell derek at psych.ward
Mon May 15 06:08:26 PDT 2006


On Mon, 15 May 2006 20:24:39 +1000, Tom S  
<h3r3tic at remove.mat.uni.torun.pl> wrote:

> Derek Parnell wrote:
>> On Mon, 15 May 2006 10:05:22 +0300, Max Samuha wrote:
>>
>>> I thought array references are similar to object references like in C#
>>> (actually, thay are object references in C#) and that was my mistake:
>>>
>>> int[] a = new int[20];
>>> int[] b = a;
>>>
>>> a.length = 40; // a is copied and b is not updated to point to a's
>>> data;
>>>
>>> Does it mean that anytime i change an array, i have to manually update
>>> all references to it or should i wrap my array in an Array class so
>>> that all references to any instance of that array remain valid?
>>>
>>> If the question have been already discussed please refer me to the
>>> right thread. Thanks
>>  I assume for some valid reason you want this behaviour...
>>   int[] a = new int[20];
>>  int[] b = a;
>>  int[] c = a;
>>  a[0] = 17;
>>  writefln("%s %s", b[0], c[0]); // Displays 17 17
>
> But... it will display '17 17'.
>
>
>> The simplest way to do this is  ...
>>   int[] a = new int[20];
>>  int[]* b = &a;
>>  int[]* c = &a;
>>  a[0] = 17;
>>  writefln("%s %s", b[0], c[0]); // Displays 17 17
>
> Nope, the earlier one :)
>
>
> As I understand it, he'd like this code:
>
> #    int[] a = new int[20];
> #    int[] b = a;
> #    b.length = 10;
> #    writefln("%s %s", a.length, b.length);
>
> to output '20 20'.

That's what you get while trying to rush out of the office, late again for  
dinner.

You are right. Here is my improved attempt and what I was trying to say  
earlier...

import std.stdio;
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?

-- 
Derek Parnell
Melbourne, Australia



More information about the Digitalmars-d-learn mailing list