Help with array maniipulation

Ali Çehreli acehreli at yahoo.com
Tue Feb 4 11:16:00 PST 2014


On 02/04/2014 10:58 AM, ollie wrote:

 > I have a C Struct:

[...]

 >         uint                    data_size;
 >         ubyte[1]                data;

Is that the C extension where the last array in a struct can have more 
elements than its size? That will be a problem in D.

 >     }
 >
 > libraw_dcraw_make_mem_thumb() returns a pointer to this struct.
 > I need to create a char[] from the data and data_size members,
 > ideally without copying, to send to GdkPixbufLoader.write(char[]).
 >
 > If I do this:
 >     libraw_processed_image_t *img;
 >     img = libraw_dcraw_make_mem_thumb();
 >     char[] buf = cast(char[]) img.data[];

This:

   char[] buf = (cast(char*)img.data.ptr)[0  .. img.data_size];

However, as I said above, data won't have room for more than 1 element.

In any case, treating a C array as a D slice is achieved by the 
following syntax:

     int[] D_slice = C_array[0 .. C_array_number_of_elements];

Ali



More information about the Digitalmars-d-learn mailing list