Access to structures defined in C

Joe jma at freedomcircle.com
Wed Sep 19 00:46:54 UTC 2018


On Tuesday, 18 September 2018 at 13:47:50 UTC, Atila Neves wrote:
> On Tuesday, 18 September 2018 at 02:39:39 UTC, Joe wrote:
>> The second type is like that shown above. The first is a 
>> simpler array of pointers to int, e.g.,
>>
>> int *yp = {2, 4, 0};
>> int *yq = {10, 12, 0};
>
> This is valid C in the sense that it compiles, but I doubt it 
> does what you think it does. This is equivalent code:
>
> int *yp = 2;
> int *yq = 10;

Sorry, Atila, I got confused looking at my two cases. I should 
have said "an array of ints", e.g.,

int yp[] = {2, 4, 0};
int yq[] = {10, 12, 0};

>> int *ys[] = {yp, yq, 0};
>
> This isn't even valid C code.

It is, because C treats 'yp' as a pointer.

>> In D, I first declared these as
>>
>> int[] yp = [2, 4];
>> int[] yq = [10, 12];
>> __gshared int*[] ys = [ &yp, &yq ];
>
> D dynamic arrays are not equivalent to C arrays.
>
> It's hard to see what you're trying to do with the code you 
> posted. Have you tried instead to use a tool to translate the C 
> headers?

At this point, I've translated everything, even the code above. I 
had to use 'immutable(int [])' in the second and higher level 
arrays like 'ys' so that they could refer to 'yp' and 'yq' 
(without the address operators).

I still have to make several changes to the library code because 
these arrays were declared as dynamic arrays of pointers, of size 
1, to bypass bounds checking. Now they're proper dynamic arrays, 
and I can use foreach on them and get other D benefits.

However, I still would like to have a deeper understanding of the 
"static variable yp cannot be read at compile time" error 
messages which went away when I declared yp immutable.


More information about the Digitalmars-d-learn mailing list