如何在C中初始化指向指针结构的指针?
我有一个结构,它是一个节点,另一个结构是这些节点的列表。在列表结构中,它是一个节点数组,但它不是一个数组,而是一个指向具有大小整数的指针的指针:
typedef struct node {
struct node *next;
MyDef *entry;
} Node;
typedef struct list {
Node **table;
int size;
} List;
List *initialize(void)
{
List *l;
Node **n;
if ((l = (List *)malloc(sizeof(List))) == NULL)
return NULL;
l->size = 11;
/* I think this is correctly allocating the memory for this 'array' of nodes */
if ((n = (Node **)malloc(l->size * sizeof(Node))) == NULL)
return NULL;
/* Now, how do I set MyDef *entry and Node *next to NULL for each of the 'array'? */
l->table = n;
return l;
}
How do I set MyDef *entry and Node *next to NULL for every 'array'?
I have a struct which is a node, and another which is a list of these nodes. In the list struct, its an array of nodes, but instead of an array, it's a pointer to pointer with a size integer:
typedef struct node {
struct node *next;
MyDef *entry;
} Node;
typedef struct list {
Node **table;
int size;
} List;
List *initialize(void)
{
List *l;
Node **n;
if ((l = (List *)malloc(sizeof(List))) == NULL)
return NULL;
l->size = 11;
/* I think this is correctly allocating the memory for this 'array' of nodes */
if ((n = (Node **)malloc(l->size * sizeof(Node))) == NULL)
return NULL;
/* Now, how do I set MyDef *entry and Node *next to NULL for each of the 'array'? */
l->table = n;
return l;
}
How do I set MyDef *entry and Node *next to NULL for each of the 'array'?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(Node **) 是指向 Node 的 [array of] 指针,因此您分配的数组不会有任何结构成员。
您应该使用 (Node *),然后您将拥有 Node 结构的指向数组,或者单独分配每个 Node,然后将指向它们的指针放入数组中。
标准 C 库中存在适合您情况的函数 calloc() :它用 0 初始化分配的区域(对应于 (char/short/int/long)0、0.0 和 NULL)。
还有内存泄漏。
当数组分配失败时,您不会释放 List,但会丢失指向它的指针。将其重写为:
因此,从我的角度来看,正确的代码将是:
此外,C99 允许您创建可变大小的结构,因此您可以像这样初始化结构
并在表中分配尽可能多的节点
malloc(sizeof(List) + sizeof(Node)*n);
(Node **) is pointer to [array of] pointer to Node, so array you allocate will not have any struct members.
You should use (Node *) and then you'll have pointed array of Node structs, or allocate each Node separately, then place pointers to them into your array.
There's exist function calloc() in standard C library for your case: it inits allocated area with 0's (which corresponds to (char/short/int/long)0, 0.0 and NULL).
Also there's a memory leak.
When array allocation fails you do not free List, but lose pointer to it. Rewrite it as:
So from my point of wiev correct code would be:
Futhermore C99 allows you to make variable size structs, so you able to init struct like
And allocate as many Node's in table as you want using
malloc(sizeof(List) + sizeof(Node)*n);
首先,在我看来,您在分配数组的代码中存在错误:它应该说
sizeof(Node*)
而不是sizeof(Node)
,因为您想要分配一个指向 Node 的指针数组而不是 Node 对象数组。然后您可以遍历数组列表:
另一个提示:您确实应该检查初始化函数是否存在内存泄漏的可能性。
First of all, it seems to me that you have an error in your code allocating the array: It should say
sizeof(Node*)
rather thansizeof(Node)
as you want to allocate an array of pointers to Node not an array of Node objects.Then you can iterate through the array list:
Another hint: You really should check your initialization function against possibilities of memory leaks.