结构中的指针数组 [英] Array of pointers in a struct

查看:55
本文介绍了结构中的指针数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我觉得我的代码在做什么不清楚,虽然它有效但是我不知道是否b / b
不确定是否有任何可能的错误,请帮助我验证它。

这是一个trie节点(就像树节点类似)struct,我存储了一个由27个指针组成的
数组和一个可以指向的空指针什么。

typedef struct trieNode

{

struct trieNode * children [27]; //儿童节点

void * obj; //存储的对象

} TrieNode;


这是我创建一个新的trie节点的代码,初始化并返回它。

TrieNode * newTrieNode()

{

int i;

//为节点分配内存

TrieNode * node =(TrieNode *)malloc(sizeof(TrieNode));

//将对象指向NULL

node-> obj = NULL;

// **********

*(node-> children)=(TrieNode *)malloc(sizeof(TrieNode *)* 27);

//将children数组中的所有指针设置为NULL

for(i = 0; i< 27; i ++)

node- > children [i] = NULL;

返回节点;

}

问题来自星号下面的行。

最初我编码

node-> children =(TrieNode **)malloc(sizeof(TrieNode *)* 27);

但编译器我不会让我编译。

当我把它改成现在的那个时,它可以工作,但对我来说似乎有点不自然

。孩子是指向指针的指针吗? *孩子或

孩子[0]是否必须由我初始化?

谢谢。

修复。

Hi all,
I feel unclear about what my code is doing, although it works but I am
not sure if there is any possible bug, please help me to verify it.
This is a trie node (just similar to tree nodes) struct, I am storing an
array of 27 pointers and a void pointer that can point to anything.
typedef struct trieNode
{
struct trieNode *children[27]; // The children nodes
void *obj; // The object stored
} TrieNode;

And this is the code I make a new trie node, initialize and return it.
TrieNode *newTrieNode()
{
int i;
// Allocate memory for the node
TrieNode *node = (TrieNode *)malloc(sizeof(TrieNode));
// points the object to NULL
node->obj = NULL;
// **********
*(node->children) = (TrieNode *)malloc(sizeof(TrieNode *) * 27);
// Set all pointers in the children array to NULL
for (i = 0; i < 27; i++)
node->children[i] = NULL;
return node;
}
The problem comes from the line below the asterisks.
Initially I have coded
node->children = (TrieNode **)malloc(sizeof(TrieNode *) * 27);
but the compiler does not let me compile.
When I change it to the one now, it works, but it seems a bit unnatural
to me. children is a pointer to pointer right? Does *children or
children[0] have to be initialized by me?
Thanks.
fix.

推荐答案

fix写道:
大家好,
我觉得我的代码在做什么不清楚,虽然它有效但是我不确定是否有任何可能的错误,请帮我验证一下。
这是一个trie节点(就像树节点一样)struct,我存储了一个
数组27个指针和一个可以指向任何东西的void指针。
typedef struct trieNode
{struct struct trieNode * children [27]; //子节点
void * obj; //存储的对象
} TrieNode;

这是我创建一个新的trie节点的代码,初始化并返回它。
TrieNode * newTrieNode()
{
int i;
//为节点分配内存
TrieNode * node =(TrieNode *)malloc(sizeof(TrieNode));
这已经被打死了。这个演员阵容不是必需的 - 这需要

必需。


TrieNode * node = malloc(sizeof(TrieNode));.

//将对象指向NULL
node-> obj = NULL;
// **********
*(node-> children)=( TrieNode *)malloc(sizeof(TrieNode *)* 27);
Hi all,
I feel unclear about what my code is doing, although it works but I am
not sure if there is any possible bug, please help me to verify it.
This is a trie node (just similar to tree nodes) struct, I am storing an
array of 27 pointers and a void pointer that can point to anything.
typedef struct trieNode
{
struct trieNode *children[27]; // The children nodes
void *obj; // The object stored
} TrieNode;

And this is the code I make a new trie node, initialize and return it.
TrieNode *newTrieNode()
{
int i;
// Allocate memory for the node
TrieNode *node = (TrieNode *)malloc(sizeof(TrieNode)); This has been beaten to death. This cast is not necessary - Do this
necessary.

TrieNode *node = malloc(sizeof(TrieNode));.
// points the object to NULL
node->obj = NULL;
// **********
*(node->children) = (TrieNode *)malloc(sizeof(TrieNode *) * 27);




这一行在这里做什么。我不明白其意义。

你的目标是什么?


-

Karthik

人类请''removeme_''查看我的真实电子邮件。



Whatz this line doing here . I dont understand the significance.
What is your objective here ?

--
Karthik
Humans please ''removeme_'' for my real email.


fix写道:
大家好,
我觉得不清楚我的代码正在做什么,虽然它有效,但我不确定是否有任何可能的错误,请帮我验证它。
这是一个trie节点(就像树节点类似)struct,我正在存储一个包含27个指针的数组和一个可以指向任何东西的void指针。
typedef struct trieNode
{struct struct trieNode * children [27]; //子节点
void * obj; //存储的对象
} TrieNode;

这是我创建一个新的trie节点的代码,初始化并返回它。
TrieNode * newTrieNode()
{
int i;
//为节点分配内存
TrieNode * node =(TrieNode *)malloc(sizeof(TrieNode));
这已经被打死了。这个演员阵容不是必需的 - 这需要

必需。


TrieNode * node = malloc(sizeof(TrieNode));.

//将对象指向NULL
node-> obj = NULL;
// **********
*(node-> children)=( TrieNode *)malloc(sizeof(TrieNode *)* 27);
Hi all,
I feel unclear about what my code is doing, although it works but I am
not sure if there is any possible bug, please help me to verify it.
This is a trie node (just similar to tree nodes) struct, I am storing an
array of 27 pointers and a void pointer that can point to anything.
typedef struct trieNode
{
struct trieNode *children[27]; // The children nodes
void *obj; // The object stored
} TrieNode;

And this is the code I make a new trie node, initialize and return it.
TrieNode *newTrieNode()
{
int i;
// Allocate memory for the node
TrieNode *node = (TrieNode *)malloc(sizeof(TrieNode)); This has been beaten to death. This cast is not necessary - Do this
necessary.

TrieNode *node = malloc(sizeof(TrieNode));.
// points the object to NULL
node->obj = NULL;
// **********
*(node->children) = (TrieNode *)malloc(sizeof(TrieNode *) * 27);




这一行在这里做什么。我不明白其意义。

你的目标是什么?


-

Karthik

人类请''removeme_''查看我的真实电子邮件。



Whatz this line doing here . I dont understand the significance.
What is your objective here ?

--
Karthik
Humans please ''removeme_'' for my real email.


2004年4月30日星期五20:47:47 -0500,修复< fi * @ here。 COM>写道:
On Fri, 30 Apr 2004 20:47:47 -0500, fix <fi*@here.com> wrote:
大家好,
我不清楚我的代码在做什么,虽然它有效,但我不确定是否有任何可能的错误,请帮我验证一下。
这是一个trie节点(就像树节点一样)struct,我存储了一个由27个指针组成的数组和一个可以指向任何东西的void指针。
typedef struct trieNode
{struct trieNode * children [27]; //子节点


这是一个包含27个指针的数组。

void * obj; //存储的对象
} TrieNode;

这是我创建一个新的trie节点的代码,初始化并返回它。
TrieNode * newTrieNode()
{
int i;
//为节点分配内存
TrieNode * node =(TrieNode *)malloc(sizeof(TrieNode));


不要从malloc投出回报。它没有帮助,可以阻止编译器警告你忘记包含stdlib.h。

这样的遗漏会导致不确定的行为。 />
//将对象指向NULL
node-> obj = NULL;
// **********
*(node-> ; children)=(TrieNode *)malloc(sizeof(TrieNode *)* 27);


这是一个问题。通过类型工作。


节点是指向struct的指针。

node-> children是一个包含27个指向struct的数组。

*(node-> children)完全等同于(node-> children)[0]

,这是该数组的第一个元素。这个元素显然是一个

指向struct的指针。

你分配的空间不足以容纳这样的结构。

结构由27个struct指针加上一个void指针加上任何

填充。你只为27个结构指针分配了足够的空间。

此时你已经犯了不可原谅的谎言,即编译器。你告诉它node-> children [0]会指向

a struct而且它没有。


除此之外,你不需要这个代码。结构已经

包含一个27个指针的数组,所以你不需要另一个。

//将children数组中的所有指针设置为NULL
for(i = 0; i< 27; i ++)
node-> children [i] = NULL;


第一次循环,你破坏你在前一个malloc中设置的
node-> children [0]中的值。这会导致内存泄漏,因为你永远无法恢复原始地址以释放内存。

返回节点;
}
问题来自星号下方的行。
最初我编码了
node-> children =(TrieNode **)malloc(sizeof(TrieNode *)* 27);


完成上述类型的工作。 node-> children是一个数组。

unsubscripted数组可能不会出现在作业的左侧

语句中。 (这不是一个可修改的左值。)


如上所述,你也不需要这个。 node->孩子已经

包含足够的空间来容纳27个指向struct的指针。

但是编译器不允许我编译。
当我将它改为现在,它有效,但对我来说似乎有点不自然。孩子是指向指针的指针吗? *孩子或
孩子[0]是否必须由我初始化?
Hi all,
I feel unclear about what my code is doing, although it works but I am
not sure if there is any possible bug, please help me to verify it.
This is a trie node (just similar to tree nodes) struct, I am storing an
array of 27 pointers and a void pointer that can point to anything.
typedef struct trieNode
{
struct trieNode *children[27]; // The children nodes
This is an array of 27 pointers.
void *obj; // The object stored
} TrieNode;

And this is the code I make a new trie node, initialize and return it.
TrieNode *newTrieNode()
{
int i;
// Allocate memory for the node
TrieNode *node = (TrieNode *)malloc(sizeof(TrieNode));
Don''t cast the return from malloc. It doesn''t help and can prevent
the compiler from warning you that you forgot to include stdlib.h.
Such an omission would lead to undefined behavior.
// points the object to NULL
node->obj = NULL;
// **********
*(node->children) = (TrieNode *)malloc(sizeof(TrieNode *) * 27);
This is a problem. Work through the types.

node is a pointer to struct.
node->children is an array of 27 pointers to struct.
*(node->children) is exactly equivalent to (node->children)[0]
which is the first element of that array. This element is obviously a
pointer to struct.
The space you allocate is not sufficient to hold such a struct.
The struct consists of 27 struct pointers plus a void pointer plus any
padding. You allocate only enough space for the 27 struct pointers.
At this point you have committed the unpardonable sin of lying to
the compiler. You have told it that node->children[0] will point to
a struct and it doesn''t.

On top of all that, you don''t need this code. The struct already
contains an array of 27 pointers so you don''t need another set.
// Set all pointers in the children array to NULL
for (i = 0; i < 27; i++)
node->children[i] = NULL;
The first time through the loop, you destroy the value in
node->children[0] that you set in the previous malloc. This causes a
memory leak because you can never recover the original address to free
the memory.
return node;
}
The problem comes from the line below the asterisks.
Initially I have coded
node->children = (TrieNode **)malloc(sizeof(TrieNode *) * 27);
Work through the types as above. node->children is an array. An
unsubscripted array may not appear on the left of an assignment
statement. (It is not a modifiable lvalue.)

As noted above you don''t need this either. node->children already
contains enough space to hold 27 pointers to struct.
but the compiler does not let me compile.
When I change it to the one now, it works, but it seems a bit unnatural
to me. children is a pointer to pointer right? Does *children or
children[0] have to be initialized by me?




是的。在你的第一个malloc(只有节点的那个)中,你可以分配

空间来保存结构。该结构由28个变量组成,27个指向struct的指针和1个指向void的指针。在评估这些变量之前,您必须初始化每个变量。这意味着孩子[0],

孩子[1],...,孩子[26]。你将指针初始化为void

以上。


当你初始化其中一个孩子时,它必须是NULL或

与结构的地址(或动态分配的内存大


<<删除电子邮件的del> ;>



Yes. In your very first malloc (the one just with node), you allocate
space to hold the struct. The struct consists of 28 variables, 27
pointers to struct and 1 pointer to void. You must initialize each of
these variables before it is evaluated. That means children[0],
children[1], ..., children[26]. You do initialize the pointer to void
above.

When you do initialize one of the children, it must be with NULL or
with the address of a struct (or dynamically allocated memory large
enough to hold a struct).

<<Remove the del for email>>


这篇关于结构中的指针数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆