How to pass a null pointer to a C function

Simen Kjærås simen.kjaras at gmail.com
Sat Nov 30 20:06:08 PST 2013


On 2013-12-01 04:46, Craig Dillabaugh wrote:
> Since questions about calling C from D seem to be popular today, I
> thought I would throw this one out there.
>
> I am trying to call a C function which takes as parameters several
> arrays of doubles.  It is valid to have some arrays passed a NULL
> pointers in the C code.
> To call this from D I've come up with the following, but it seems like a
> bit of a hack.
>
> double[] x1 = [ 2.4, 3.7, 9.7, 4.5 ];
> double[] y1 = [ 2.4, 9.8, 9.1, 3.4 ];
> double[] empty;
>
> SHPObject*[] shape_ptrs;
> shape_ptrs ~= SHPCreateSimpleObject( SHPT_POLYGON, to!int(x1.length),
>                         x1.ptr, y1.ptr, empty.ptr );
>
> It should be clear what is going on.  A POLYGON is defined as a set of
> points (provided as arrays of doubles), with X,Y and optional Z
> coordinates.
> In this case I want a 2D polygon, so I want the final Z array to be
> empty.
>
> I wanted to just write 0 as the final parameter, but that didn't work, so
> I tried to!(double*)(0), and that didn't make the compiler happy either.
>
> I guess using the empty.ptr bit makes sense, in that the final array is
> meant to be empty, so pass it a pointer to an empty array.  But it seems
> a bit hackish that I need to declare an additional empty array just to
> call this function.
>
> Is there an accepted 'proper' way of passing NULL pointers to C
> functions from D?

Well, there is null.

--
   Simen


More information about the Digitalmars-d-learn mailing list