Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create static array of arrays with different lengths [closed]

Tags:

arrays

c

static

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 ...

like image 662
kolage Avatar asked Nov 21 '13 09:11

kolage


People also ask

Can static array have variable size?

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.

Can you make an array static?

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.

Can you change values of static array in Java?

You can easily edit the information in the static String[]. Then you can manipulate a specific entry as you please.

What is static in array?

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# −


2 Answers

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.

like image 104
Kerrek SB Avatar answered Sep 28 '22 15:09

Kerrek SB


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.

like image 41
Jens Gustedt Avatar answered Sep 28 '22 16:09

Jens Gustedt