Numeric access to char[]

Peter Thomassen info at peter-thomassen.de
Wed Aug 23 19:33:53 PDT 2006


Regan Heath schrieb am Donnerstag, 24. August 2006 04:21:
>> Thanks, this is what im looking for! But I don't understand this line:
>>> val = (cast(int*)c.ptr)[0..1][0];
>>
>> What does [0..1][0] mean?
> 
> Ahh.. to understand the line we need to know that D allows us to slice
> pointers and the result is an array, lets break the line into steps.
> 
> Step1: cast(int*)c.ptr, this tells it to pretend the pointer to the char[]
> data is an int pointer.
> 
> Step2: [0..1], this requests a slice (AKA array) of the data referenced by
> the (now) int pointer. The result is an int[], in this case with only 1
> item, a single int.
> 
> Step3: [0] returns the first item (only item) in the int slice (array), an
> int.

Okay, nearly understood. But doesn't [0..1] produce two items, namely [0]
and [1]?

Peter



More information about the Digitalmars-d-learn mailing list