Thread overview | ||||||
---|---|---|---|---|---|---|
|
December 01, 2013 How to pass a null pointer to a C function | ||||
---|---|---|---|---|
| ||||
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? |
December 01, 2013 Re: How to pass a null pointer to a C function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Dillabaugh | 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 );
|
December 01, 2013 Re: How to pass a null pointer to a C function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Craig Dillabaugh | 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
|
December 01, 2013 Re: How to pass a null pointer to a C function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjærås | 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.
|
Copyright © 1999-2021 by the D Language Foundation