Author Topic: Struct wrapping (multi-dimensional) arrays  (Read 1472 times)

shiv

  • Newbie
  • *
  • Posts: 12
    • View Profile
Struct wrapping (multi-dimensional) arrays
« on: December 18, 2020, 05:18:35 PM »
Gnu C extension allows:
void * foo (int n) {
struct S { int data[n]; };
int (*s)[n] = malloc( sizeof (int[n]) );
...
}
which is very convenient. It would be even better to allow support for the following:
struct S {
int n;
f32 (*data)[.n]; // the ".n" refers to the internal field
}

It would be even more awesome to allow wrapping multi-dimensional arrays:
struct S2 {
int m;
int n;
f32 (*d)[.m][.n];
}
struct S2 a2;
a2.m = 100;
a2.n = 50;
a2.d = malloc( sizeof( f32[a2.m][a2.n] );
(*a2.d)[0][0] = (*a2.d)[1][1] + (*a2.d)[2][3];

If we then combine with my previous suggestion of allowing array access syntax on "fat pointers" we could do:
(*a2)[0][0] = (*a2)[1][1] + (*a2)[2][3];