Any way to set len and ptr in dyn arrays?
James Dunne
james.jdunne at gmail.com
Mon Aug 7 15:46:31 PDT 2006
nobody wrote:
> The only way I have found is to use a struct/union kludge:
>
> // redefine the dyn array struct
> struct sArrayKludge
> {
> int len;
> void* ptr;
> }
>
> // use a union to allow reaching inside
> union uArrayKludge
> {
> byte[] myArray;
> sArrayKludge myArrayInternals;
> }
>
> byte someFunc()
> {
> // declare the union where I would declare myArray
> uArrayKludge theArray;
>
> int[] buf = cast(byte[]) read("file.ext");
>
> // use the union where I would use the array
> theArray.myArrayInternals.len = buf[0] * buf[1];
> theArray.myArrayInternals.ptr = &buf[2];
>
> // now I can use the ith byte
> return theArray.myArray[buf[0]+buf[1]];
> }
>
>
>
> Thanks in advance.
This is very interesting and all, but why would you do such a thing?
Is the .length property on the dynamic array just not cuttin' it for you?
--
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/MU/S d-pu s:+ a-->? C++++$ UL+++ P--- L+++ !E W-- N++ o? K? w--- O
M--@ V? PS PE Y+ PGP- t+ 5 X+ !R tv-->!tv b- DI++(+) D++ G e++>e
h>--->++ r+++ y+++
------END GEEK CODE BLOCK------
James Dunne
More information about the Digitalmars-d-learn
mailing list