casting and arrays..

Reiner Pope some at address.com
Sat Mar 10 21:30:49 PST 2007


gareis wrote:
> Then you need to do pointer arithmetic, it seems. Assuming your struct 
> has a deserializer method that takes a void*:
> ---
> for (int i = 0; i < array.length; i++) {
>     if (someStructStartsHere()) {
>         someStruct s;
>         s.deserialize(array.ptr + i);
>         do_stuff_with_someStruct (s);
>     }
> }
> ---
> 
Let D do the memcpy?

foreach (i, elem; array)
{
     if (someStructStartsHere()) {
         someStruct s = * (cast(someStruct*) &array[i]);
     }
}

That should copy the struct out of the array.

Cheers,

Reiner


More information about the Digitalmars-d-learn mailing list