CrispyCashew - 1 year ago 119
C Question

# Malloc an array of an array of structs

Objective

Dynamically create an array of an array of Element strucs (defined below)

``````typedef struct {
void* data;
} Element;
``````

Question

I know how to malloc an array of Element strucs

``````Element* arrayOfElements = malloc(4 * sizeof(Element));
``````

But then how do I Malloc an array of the above? (an array of arrayOfElements)

Question 2

Lets say the array of arrayOfElements is called arrayOfArrayStruc how would I proceed to set values inside of it

For Example I want to copy 65 to arrayOfElements[2] which is inside arrayOfArrayStruc1 how would I got about that ?

I know how to do that if I wanted to copy 65 straight to arrayOfElements[2]

``````arrayOfElements[2].data = malloc( sizeof(int) );
ptr = arrayOfElements[2].data;
*ptr = 65;
``````

but im not sure how to do that if arrayOfElements[2] is inside arrayOfArrayStruc1.

EDIT

To make it more clear my goal i've made a picture

So in green is the structure Element defined by

``````typedef struct {
void* data;
} Element;
``````

Then in red ( which had 4 green boxes) is an Array of Element structures which I malloc'd using

``````Element* arrayOfElements = malloc(4 * sizeof(Element));
``````

What im looking to do is store the above ^^ in an array or make an array of pointers (which is the blue box with red boxes in it)

So in the picture "Array Of Element" holds 4 Element Structures, then I want to make an array to store 4 "Array Of Element" (or an array of 4 pointers to point to each "Array of Element")

If you want an array of Element* then you could do something like this where is `n` is the number of pointers

``````Element** arrayOfStructs = malloc( n* sizeof(Element*) );
``````

so for n = 4; you get an array of 4 pointers

arrayOfStructs

``````+---+
|   | ->
+---+
|   | ->
+---+
|   | ->
+---+
|   | ->
+---+
``````

Now allocate for each entry in the arrayOfStructs so if `m` is the number of Elements

``````for (int i = 0; i < n; ++i)
{
arrayOfStructs[i] = malloc(m * sizeof(Element));
}
``````

Since each element has a data pointer, you need to allocate what that points to as well

``````for (int i = 0; i < n; ++i)
{
arrayOfStructs[i] = malloc(m * sizeof(Element));
for (int j = 0; j < m; ++j)
{
arrayOfStructs[i][j].data = malloc(sizeof(int));
}
}
``````

After this you will have the following in memory

Lets say m = 3;

arrayOfStructs

``````+---+    +---+---+----+
|   | -> |   |   |    |  array of Elements
+---+    +---+---+----+    +---+---+----+
|   | -------------------> |   |   |    |
+---+    +---+---+----+    +---+---+----+
|   | -> |   |   |    |
+---+    +---+---+----+    +---+---+----+
|   | -------------------> |   |   |    |
+---+                      +---+---+----+
``````

each element in "array of Elements" 1..3(or rather 0..2) point to a different "data" (below on array of Elements is turned around 90 degrees so I can more easily draw boxes)

``````+---+    +---+
|   | -> |   | integer
+---+    +---+   +---+
|   | ---------> |   |
+---+            +---+     +---+
|   | -------------------> |   |
+---+                      +---+
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download