在c中管理结构集合
我正在学习c,遇到了一个存储和管理结构的问题;让我解释一下 我非常熟悉Java,并且开始理解如何在c中正确使用指针和引用。我的目标是制作和储存数百件物品:在c中管理结构集合,c,arrays,pointers,struct,C,Arrays,Pointers,Struct,我正在学习c,遇到了一个存储和管理结构的问题;让我解释一下 我非常熟悉Java,并且开始理解如何在c中正确使用指针和引用。我的目标是制作和储存数百件物品: struct creature{ int id; char name[12]; } 在java中,我可以创建如下数组: creature c []; 然后在该数组中存储和操作对象。我希望在c中使用结构“生物”也能达到同样的效果 我的第一次尝试是: creature *creatures = malloc(sizeof(creatu
struct creature{
int id;
char name[12];
}
在java中,我可以创建如下数组:
creature c [];
然后在该数组中存储和操作对象。我希望在c中使用结构“生物”也能达到同样的效果
我的第一次尝试是:
creature *creatures = malloc(sizeof(creature));
我甚至不完全确定这会起什么作用。我的目标是创建一个严格保存结构指针的内存块,比如Java数组
从这里,我想创建新的生物结构,并将它们的指针存储在bioters变量中,该变量使用malloc分配了内存
我希望这些信息足以解释我的问题
谢谢 你就快到了。将要分配的字节数作为参数,因此需要将结构的大小乘以要分配的结构的数量:
int number_of_creatures = 10;
creature *creatures = malloc(sizeof(creature) * number_of_creatures);
您还应该始终检查从malloc()
返回的指针,以检查分配是否成功
if (creatures == NULL)
printf("Allocating memory failed!\n");
你就快到了。将要分配的字节数作为参数,因此需要将结构的大小乘以要分配的结构的数量:
int number_of_creatures = 10;
creature *creatures = malloc(sizeof(creature) * number_of_creatures);
您还应该始终检查从malloc()
返回的指针,以检查分配是否成功
if (creatures == NULL)
printf("Allocating memory failed!\n");
好的,那么您需要的是一个指针数组。请注意,在Java中,只要您有以下内容:
String s = ...;
这将转换为c中的指针:
String *s = ...;
让我们先来看一个简单的例子,固定大小的指针数组。这很简单:
creature *creatures[10];
for (var i=0; i < 10; i++) {
creatures[i] = malloc(sizeof(creature));
}
也就是说,数组表示为指向第一个元素的指针。您现在需要的是一个指针数组,它最终成为指向指针的指针:
var numCritters = 10;
creature **creatures = malloc(sizeof(creature *) * numCritters);
for (var i=0; i < numCritters; numCritters++) {
creatures[i] = malloc(sizeof(creature));
}
而我创建了一个指针数组,每个指针指向一个生物
:
+--------------------+
| |
creature *creatures = +------+ | |
| ptr0 | ----> | Creature 0 |
+--------------------+ +------+ | |
| | +-- | ptr1 | | |
| | | +------+ +--------------------+
| Creature 1 | <-+ | ptr2 | --+
| | +------+ | +--------------------+
| | | .... | | | |
+--------------------+ | | |
+-> | Creature 2 |
| |
| |
+--------------------+
+--------------------+
| |
生物*生物=+----+||
|ptr0--->|生物0|
+--------------------+ +------+ | |
||+——|ptr1||
| | | +------+ +--------------------+
|生物1 | |生物2|
| |
| |
+--------------------+
没错,所以您需要的是一个指针数组。请注意,在Java中,只要您有以下内容:
String s = ...;
这将转换为c中的指针:
String *s = ...;
让我们先来看一个简单的例子,固定大小的指针数组。这很简单:
creature *creatures[10];
for (var i=0; i < 10; i++) {
creatures[i] = malloc(sizeof(creature));
}
也就是说,数组表示为指向第一个元素的指针。您现在需要的是一个指针数组,它最终成为指向指针的指针:
var numCritters = 10;
creature **creatures = malloc(sizeof(creature *) * numCritters);
for (var i=0; i < numCritters; numCritters++) {
creatures[i] = malloc(sizeof(creature));
}
而我创建了一个指针数组,每个指针指向一个生物
:
+--------------------+
| |
creature *creatures = +------+ | |
| ptr0 | ----> | Creature 0 |
+--------------------+ +------+ | |
| | +-- | ptr1 | | |
| | | +------+ +--------------------+
| Creature 1 | <-+ | ptr2 | --+
| | +------+ | +--------------------+
| | | .... | | | |
+--------------------+ | | |
+-> | Creature 2 |
| |
| |
+--------------------+
+--------------------+
| |
生物*生物=+----+||
|ptr0--->|生物0|
+--------------------+ +------+ | |
||+——|ptr1||
| | | +------+ +--------------------+
|生物1 | |生物2|
| |
| |
+--------------------+
下次请在帖子中包含一个实际问题;)。请下次在你的帖子中加入一个实际问题;)。最好是建议新来者c
不要抛出malloc@Pankrates:怎么会呢?工藤支持ASCII图表:)@Pankrates:呵呵,谢谢。演员阵容很有趣,我从来不知道。有道理,我会更新答案。也许最好建议新来者使用c
不要使用malloc@Pankrates:怎么会呢?工藤支持ASCII图表:)@Pankrates:呵呵,谢谢。演员阵容很有趣,我从来不知道。有道理,我会更新答案thank you@pankrates为我清除malloc的大小参数。thank you@pankrates为我清除malloc的大小参数。