C-binding external array.

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Aug 9 07:01:17 PDT 2016


On 8/9/16 9:53 AM, ciechowoj wrote:
> Is there a way to access a static array from D without knowing the size
> of the array?
>
> Let suppose there is an array, somewhere in lib.c.
>
> int tab[64];
>
> and there is header file lib.h with following reference:
>
> extern int tab[];
>
> How to declare `tab` in the D code without knowing the size of the
> array? In other words, how to map external declaration of the `tab` to
> the D code?

I think this should work:

extern extern(C) int[1] tab;

Then if you want to access the elements, use the tab.ptr[elem]

If it's a global variable, tack on __gshared.

-Steve


More information about the Digitalmars-d-learn mailing list