Malloc结构数组的数组

时间:2022-01-01 00:59:32

Objective

目的

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

动态创建Element strucs数组的数组(在下面定义)

typedef struct {
    void* data;
    } Element;

Question

I know how to malloc an array of Element strucs

我知道如何malloc一个Element结构数组

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

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

但那么我怎么把Malloc上面的数组呢? (arrayOfElements数组)

Question 2

问题2

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

让我们说arrayOfElements数组被称为arrayOfArrayStruc我将如何继续设置其中的值

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

例如,我想将65复制到arrayOfErrayStruc1中的arrayOfElements [2],我将如何得到它?

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

如果我想直接复制65到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.

但如果arrayOfElements [2]在arrayOfArrayStruc1中,我不知道怎么做。

EDIT

编辑

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

为了让我的目标更清晰,我已经拍了一张照片

Malloc结构数组的数组

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

然后用红色(有4个绿色框)是我用malloc使用的Element结构数组

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")

因此,在图片“元素阵列”中保存4个元素结构,然后我想制作一个数组来存储4个“元素阵列”(或指向每个“元素阵列”的4个指针数组)

2 个解决方案

#1


1  

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

如果你想要一个Element *数组,那么你可以做这样的事情,其中​​n是指针的数量

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

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

所以对于n = 4;你得到一个4指针的数组

arrayOfStructs

arrayOfStructs

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

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

现在为arrayOfStructs中的每个条目分配,如果m是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;

可以说m = 3;

arrayOfStructs

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)

“元素数组”1..3(或更确切地说是0..2)中的每个元素指向不同的“数据”(元素数组下方转动90度左右,因此我可以更轻松地绘制框)

+---+    +---+
|   | -> |   | integer
+---+    +---+   +---+
|   | ---------> |   |
+---+            +---+     +---+
|   | -------------------> |   |
+---+                      +---+

#2


1  

arrayOfElements is the name of a pointer variable. You cannot have an array of names.

arrayOfElements是指针变量的名称。你不能拥有一系列名字。

You could have an array of pointer variables. You can write the code for that, it is the same as the code for an array of int, but use a pointer type instead of int. Then, you would need to initialize each of those pointer variables in the array the same way as you are doing now.

你可以有一个指针变量数组。您可以为此编写代码,它与int数组的代码相同,但使用指针类型而不是int。然后,您需要以与现在相同的方式初始化数组中的每个指针变量。

However, as posted, the question asked for "array of arrays", not "array of pointers". An "array of arrays" is an array where the element type is an array (not a pointer).

然而,如发布,问题是“数组数组”,而不是“指针数组”。 “数组数组”是一个数组,其中元素类型是数组(不是指针)。

Here is a non-dynamically-allocated array: int x[4][5]; . This is an array of 4 elements, with each element being an array of 5 ints.

这是一个非动态分配的数组:int x [4] [5]; 。这是一个包含4个元素的数组,每个元素都是5个int的数组。

To dynamically allocate one of these, it is the same code as dynamically allocating any array of 4 elements. We just use int[5] as the element type, instead of int or whatever.

要动态分配其中一个,它与动态分配4个元素的任何数组的代码相同。我们只使用int [5]作为元素类型,而不是int或其他。

The type of the pointer to the first element is: "pointer to int[5]". In C syntax this is written int (*)[5] -- not int *[5] which is an array of pointers.

指向第一个元素的指针的类型是:“指向int [5]的指针”。在C语法中,这是写入int(*)[5] - 而不是int * [5]这是一个指针数组。

One way to write the code would be:

编写代码的一种方法是:

int (*px)[5] = malloc(4 * sizeof(int[5]));

hopefully you can see the similarity between this and the malloc in your question. We just replaced Element with int[5]. (So, your job now, is to use Element[5] instead of int[5]. Or whatever size instead of 5).

希望你能在你的问题中看到这和malloc之间的相似之处。我们刚用int [5]替换了Element。 (所以,你现在的工作是使用Element [5]而不是int [5]。或者是任何大小而不是5)。

To avoid repetition (and so avoid the possibility of errors) it's possible to use the common idiom:

为了避免重复(并避免错误的可能性),可以使用常用的习语:

int (*px)[5] = malloc(4 * sizeof *px);

which is 4 elements each of the right size for what the pointer is pointing to.

这是4个元素,每个元素都是指针指向的正确大小。

#1


1  

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

如果你想要一个Element *数组,那么你可以做这样的事情,其中​​n是指针的数量

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

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

所以对于n = 4;你得到一个4指针的数组

arrayOfStructs

arrayOfStructs

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

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

现在为arrayOfStructs中的每个条目分配,如果m是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;

可以说m = 3;

arrayOfStructs

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)

“元素数组”1..3(或更确切地说是0..2)中的每个元素指向不同的“数据”(元素数组下方转动90度左右,因此我可以更轻松地绘制框)

+---+    +---+
|   | -> |   | integer
+---+    +---+   +---+
|   | ---------> |   |
+---+            +---+     +---+
|   | -------------------> |   |
+---+                      +---+

#2


1  

arrayOfElements is the name of a pointer variable. You cannot have an array of names.

arrayOfElements是指针变量的名称。你不能拥有一系列名字。

You could have an array of pointer variables. You can write the code for that, it is the same as the code for an array of int, but use a pointer type instead of int. Then, you would need to initialize each of those pointer variables in the array the same way as you are doing now.

你可以有一个指针变量数组。您可以为此编写代码,它与int数组的代码相同,但使用指针类型而不是int。然后,您需要以与现在相同的方式初始化数组中的每个指针变量。

However, as posted, the question asked for "array of arrays", not "array of pointers". An "array of arrays" is an array where the element type is an array (not a pointer).

然而,如发布,问题是“数组数组”,而不是“指针数组”。 “数组数组”是一个数组,其中元素类型是数组(不是指针)。

Here is a non-dynamically-allocated array: int x[4][5]; . This is an array of 4 elements, with each element being an array of 5 ints.

这是一个非动态分配的数组:int x [4] [5]; 。这是一个包含4个元素的数组,每个元素都是5个int的数组。

To dynamically allocate one of these, it is the same code as dynamically allocating any array of 4 elements. We just use int[5] as the element type, instead of int or whatever.

要动态分配其中一个,它与动态分配4个元素的任何数组的代码相同。我们只使用int [5]作为元素类型,而不是int或其他。

The type of the pointer to the first element is: "pointer to int[5]". In C syntax this is written int (*)[5] -- not int *[5] which is an array of pointers.

指向第一个元素的指针的类型是:“指向int [5]的指针”。在C语法中,这是写入int(*)[5] - 而不是int * [5]这是一个指针数组。

One way to write the code would be:

编写代码的一种方法是:

int (*px)[5] = malloc(4 * sizeof(int[5]));

hopefully you can see the similarity between this and the malloc in your question. We just replaced Element with int[5]. (So, your job now, is to use Element[5] instead of int[5]. Or whatever size instead of 5).

希望你能在你的问题中看到这和malloc之间的相似之处。我们刚用int [5]替换了Element。 (所以,你现在的工作是使用Element [5]而不是int [5]。或者是任何大小而不是5)。

To avoid repetition (and so avoid the possibility of errors) it's possible to use the common idiom:

为了避免重复(并避免错误的可能性),可以使用常用的习语:

int (*px)[5] = malloc(4 * sizeof *px);

which is 4 elements each of the right size for what the pointer is pointing to.

这是4个元素,每个元素都是指针指向的正确大小。