www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to pass a null pointer to a C function

reply "Craig Dillabaugh" <craig.dillabaugh gmail.com> writes:
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?
Nov 30 2013
next sibling parent Mike Parker <aldacron gmail.com> writes:
On 12/1/2013 12:46 PM, Craig Dillabaugh wrote:

 Is there an accepted 'proper' way of passing NULL pointers to C
 functions from D?
shape_ptrs ~= SHPCreateSimpleObject( SHPT_POLYGON, to!int(x1.length), x1.ptr, y1.ptr, null );
Nov 30 2013
prev sibling parent reply =?UTF-8?B?U2ltZW4gS2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
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
Nov 30 2013
parent "Craig Dillabaugh" <craig.dillabaugh gmail.com> writes:
On Sunday, 1 December 2013 at 04:11:57 UTC, Simen Kjærås wrote:
 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
Simen and Mike. Thanks! Now I feel stupid.
Nov 30 2013