Any way to set len and ptr in dyn arrays?

Frank Benoit keinfarbton at nospam.xyz
Tue Aug 8 04:50:24 PDT 2006


nobody schrieb:
> Tom S wrote:
>> nobody wrote:
>>>   theArray.myArrayInternals.len = buf[0] * buf[1];
>>>   theArray.myArrayInternals.ptr = &buf[2];
>>
>> erm ...
>>
>> foo = (&buf[2])[0 .. buf[0] * buf[1]];
> 
> Thanks for your answer. I understand that would ultimately have the same
> result. My question is about whether that directly sets the len and ptr
> fields or are does more stuff happen. If so what is it? If not then why
> is there no direct access to the ptr and len fields?
> 

This builds a slice, which is like setting ptr/length of an array. This
is called slicing. Instead of Toms example, I think you can also write this:
foo = buf[ 2 .. buf[0] * buf[1] + 2 ];

Refering to http://www.digitalmars.com/d/arrays.html
slicing:
b = a[1..3];
array copy:
s[] = t[];
s[1..2] = t[0..1];



More information about the Digitalmars-d-learn mailing list