Any way to set len and ptr in dyn arrays?

nobody nobody at mailinator.com
Mon Aug 7 11:25:05 PDT 2006


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.



More information about the Digitalmars-d-learn mailing list