I need to create array of arrays in static memory, but with different row lengths. I can compute sizes of each row at compile time, but I don't know how to write it down or if it is even possible.
Any ideas please? Thanks in advance ...
I need to create array of arrays in static memory, but with different row lengths. I can compute sizes of each row at compile time, but I don't know how to write it down or if it is even possible.
Any ideas please? Thanks in advance ...
You can't have an array of arrays, because arrays can only have one single type of element, and T[N]
and T[M]
are different types.
However, you can have an array of pointers:
T a0[5], a1[7], a2[21], a3[2];
T * arr[] = { a0, a1, a2, a3 };
Now you can use arr[0][i]
etc.
You can use an array of pointers that you initialize with compound literals
double* A[] = {
(double[]){ init00, init012, [45] = init3, },
(double[]){ init10, init11, init3 },
(double[34]){ 0.0 },
};
As long as you can guarantee that the initializers and sizes are known at compile time all these allocations will be done statically. The compound literals avoid you to have to declare temporary variables and to polute the namespace of your program.