C-binding external array.
ciechowoj via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Aug 9 11:46:09 PDT 2016
On Tuesday, 9 August 2016 at 15:41:08 UTC, Steven Schveighoffer
wrote:
>
> Well, you can via properties:
>
> @property int* tabp() { return tab.ptr; }
>
> tabp[elem];
This is nice. The best would be to have it with the same name as
original symbol, but I can't imagine how it could be done.
> Essentially, tab is a symbol that points at some undetermined
> number of elements. Since it's undetermined, D doesn't allow
> safe easy access.
>
> If you did int *tab, then it would think the symbol points at a
> pointer.
>
> tab.ptr is a shortcut to &tab[0].
>
> You could potentially do int tab, and then use (&tab)[elem].
>
> Or if you know the number of elements, you can just declare
> them.
>
> If it were me, I'd access it via tab.ptr, because it *is* an
> unsafe operation and I'd want to highlight that for future
> readers.
>
> If something defines tab's length, I'd highly recommend
> wrapping the two:
>
> extern(C) int tabLength(); // mythical mechanism or no?
>
> @property int[] dtab { return tab.ptr[0 .. tabLength]; }
And this is even better. However, I suppose you are right and I
should stick to `tab.ptr`.
More information about the Digitalmars-d-learn
mailing list