c - 在c中为结构指针数组声明内存

标签 c arrays pointers struct

我在 c 中有一个结构,如下所示:

typedef struct edgenode
{
   int value;
   struct edgenode * next;
};

我希望创建一个边缘节点指针数组。 所以,我可以做edgenode * array[50]。

但是,我该如何为此动态分配内存呢? 会不会,

edgenode ** array = malloc(sizeof(edgenode)*50)?

最佳答案

你可以这样做

edgenodes * array = malloc(50 * sizeof *array);

您可以通过NULL初始化它,如下

for(i = 0; i < 50; ++i)
  array[i] = NULL;

为您需要的每个实例分配内存

for(i = 0; i < 50; ++i)
  array[i] = malloc(sizeof *array[i]);

关于c - 在c中为结构指针数组声明内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24263473/

相关文章:

Javascript 在数组中存储多个字段

php - array_diff() 无明显原因失败

c - 写数据到寄存器

java - JNA : Pointer to array of Struct : Invalid Pointer

c - 通过 printf 格式化字符串输出

c - 什么 ABI(如果有的话)限制 [u]intmax_t 的大小?

c - 如何重现 __builtin___memset_chk

android - 在 gdb 中看不到任何函数

Java 2D 数组 - 输入值

c - 如何动态分配指针数组?