decide to use byte[] bytesof = tobytes((&structvar)[0..1]);

redsea redsea at 163.com
Sun Nov 18 02:28:15 PST 2007


This seems work with static arary,  the following code give result 36.

Not very simple, but typesafe.

Thanks,  everybody!
 

struct SS
{
    int i;
    int j;
    int k;
}


byte[]  tobytes(void [] v)
{
    return cast(byte []) v;
}

int main()
{
    SS[3] structvar;

    byte[] bytesof = tobytes((&structvar)[0..1]);
    Stdout(bytesof.length).newline;

    return 0;
}

Lionello Lunesu Wrote:

> The cast to void[] is implicit:
> 
> void[] bytesof = (&structvar)[0..1];
> 
> and there's no need for the .sizeof if you leave the pointer of type 
> struct*.
> 
> It also makes sense to make the cast to byte[] NOT implicit, since the 
> resulting array might not be portable on a system with different endianess.
> 
> L. 
> 
> 




More information about the Digitalmars-d mailing list