two-dimensional C array and its analog in D

Alexandr Druzhinin drug2004 at bk.ru
Wed Aug 8 10:07:27 PDT 2012


08.08.2012 22:21, Ali Çehreli пишет:
>
> I looked at its online documentation: count is also an array that tells
> the lengths of individual rows of indices, right? So in reality the data
> is a dynamic ragged array? (I've never used that function before.)

Yes, it is.

>
>  > If I declare indices like
>  > uint[][] indices;
>
> That's a slice of uint slices. Completely different memory layout than
> static arrays.

Ok. I'll read about slices once again

> In any case, I am pretty sure that what you need is the .ptr property of
> D arrays. You will have to make the 'indices' parameter dynamically by
> calling .ptr on the slices.

.ptr works fine. if I do, for example so:

uint[] firstSubArray;
uint[] secondSubArray;

uint*[] indicies;
indicies[0] = firstSubArray.ptr;
indicies[1] = secondSubArray.ptr;

that is I manually form array of pointers. But I don't like it.

>
> I've started writing the following but I don't know how you are calling
> the function. Can you get this to do what you expect in C:
>
> // WARNING: THIS C CODE DOES NOT COMPILE.
> #include <stdio.h>
>
> typedef size_t sizei;
>
> void glMultiDrawElements(
>      /* enum mode,*/
>      sizei *count,
>      /* enum type,*/
>      void **indices,
>      sizei primcount)
> {
>      for (size_t i = 0; i != primcount; ++i) {
>          for (size_t j = 0; j != count[i]; ++j) {
>              printf(" %d", indices[i][j]);
>          }
>          printf("\n");
>      }
> }
>
> int main()
> {
>      /* Normally, the count array would be generated dynamically. */
>      int counts[4] = { 3, 3, 3, 3 };
>
>      int data[4][3];
>      data[0][0] = 42;
>      data[2][2] = 43;
>
>      glMultiDrawElements(counts, data, 4);
> }

yes, it looks like what I mean, but I'm sleepy now :)



More information about the Digitalmars-d-learn mailing list