Communication between D and C with dynamic arrays

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 10 08:08:20 PDT 2014


On Sun, 10 Aug 2014 14:26:27 +0000
seany via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com> wrote:

> I am having difficulty in imagining how to send D arrays to a C 
> function.
do something like this:

=== C SIDE ===
void c_array_processing (int *items, size_t item_count) {
  // use items pointer as normal C-like array pointer
  ...
}

=== D SIDE ===
extern(C) void c_array_processing (int* items, size_t item_count);

int[] arr;
...
arr ~= 42;
...
c_array_processing(arr.ptr, arr.length);


> PS: is there a built in size operator for arrays in D?
if you want to get size of all array items, do this:

size_t dataSize = arr[0].sizeof*arr[0].length;

if you want to get the size of the internal array structure... just
don't do that. it's better to not use internal compiler and runtime
structures.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20140810/fa2d836f/attachment.sig>


More information about the Digitalmars-d-learn mailing list