Re: C-binding external array.

2016-08-10 Thread ciechowoj via Digitalmars-d-learn
On Wednesday, 10 August 2016 at 15:07:52 UTC, Ali Çehreli wrote: On 08/10/2016 02:05 AM, ciechowoj wrote: Better with some mixin magic: mixin template CArray(string symbol, T) { pragma(mangle, symbol) extern extern(C) __gshared mixin ("T[0] _c" ~ symbol ~ ";"); @property mixin (

Re: C-binding external array.

2016-08-10 Thread Ali Çehreli via Digitalmars-d-learn
On 08/10/2016 02:05 AM, ciechowoj wrote: On Tuesday, 9 August 2016 at 19:16:42 UTC, Steven Schveighoffer wrote: D has an answer: pragma(mangle, "tab") extern extern(C) int[1] _ctab; @property int* tab() { return _ctab.ptr; } I still don't recommend doing this, for previously stated reasons.

Re: C-binding external array.

2016-08-10 Thread ciechowoj via Digitalmars-d-learn
On Tuesday, 9 August 2016 at 19:16:46 UTC, Ali Çehreli wrote: Well, C's array symbol is used as a pointer to the first element and D allows array indexing for pointers as well. Here is the C code: // c.c #include "stdio.h" int tab[64]; int *get() { return tab;// or &tab[0] } void in

Re: C-binding external array.

2016-08-10 Thread ciechowoj via Digitalmars-d-learn
On Tuesday, 9 August 2016 at 19:16:42 UTC, Steven Schveighoffer wrote: D has an answer: pragma(mangle, "tab") extern extern(C) int[1] _ctab; @property int* tab() { return _ctab.ptr; } I still don't recommend doing this, for previously stated reasons. This is really interesting :).

Re: C-binding external array.

2016-08-09 Thread Ali Çehreli via Digitalmars-d-learn
On 08/09/2016 11:46 AM, ciechowoj wrote: > On Tuesday, 9 August 2016 at 15:41:08 UTC, Steven Schveighoffer wrote: >> @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

Re: C-binding external array.

2016-08-09 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/9/16 2:46 PM, ciechowoj wrote: 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 imagin

Re: C-binding external array.

2016-08-09 Thread ciechowoj via Digitalmars-d-learn
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. Essentiall

Re: C-binding external array.

2016-08-09 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/9/16 11:41 AM, Steven Schveighoffer wrote: extern(C) int tabLength(); // mythical mechanism or no? @property int[] dtab { return tab.ptr[0 .. tabLength]; } And I've used mythical syntax also! Should be: @property int[] dtab() { return tab.ptr[0 .. tabLength]; } -Steve

Re: C-binding external array.

2016-08-09 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/9/16 10:09 AM, ciechowoj wrote: On Tuesday, 9 August 2016 at 14:01:17 UTC, Steven Schveighoffer wrote: 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. I've already trie

Re: C-binding external array.

2016-08-09 Thread ciechowoj via Digitalmars-d-learn
On Tuesday, 9 August 2016 at 14:25:15 UTC, Kagamin wrote: Well extern extern(C) __gshared int[64] tab; My assumption is you do not know the size of the array.

Re: C-binding external array.

2016-08-09 Thread Kagamin via Digitalmars-d-learn
Well extern extern(C) __gshared int[64] tab;

Re: C-binding external array.

2016-08-09 Thread ciechowoj via Digitalmars-d-learn
On Tuesday, 9 August 2016 at 14:01:17 UTC, Steven Schveighoffer wrote: 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 I've already tried this and works (64-bit at lea

Re: C-binding external array.

2016-08-09 Thread Steven Schveighoffer via Digitalmars-d-learn
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 c

C-binding external array.

2016-08-09 Thread ciechowoj via Digitalmars-d-learn
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