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 cannot declare a static array of variable size because its space is allocated in the Data Segment (or bss segment in case of an uninitialized variable). Hence the compiler needs to know the size at the compile time and will complain if the size is not a constant.
An array that is declared with the static keyword is known as static array. It allocates memory at compile-time whose size is fixed. We cannot alter the static array. If we want an array to be sized based on input from the user, then we cannot use static arrays.
You can easily edit the information in the static String[]. Then you can manipulate a specific entry as you please.
A static array is a data structure with a fixed size. Let us see an example of a static array in C#. Here is a static string array. The data remains the same here i.e. fixed − static string[] _fruits = new string[] { "apple", "mango" }; Now let us see the complete example to create and access static arrays in C# −
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With