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

问题描述

大家好,

我觉得我的代码在做什么不清楚,虽然它有效但是我不知道是否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.

推荐答案




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

你的目标是什么?


-

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.





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

你的目标是什么?


-

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.





是的。在你的第一个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>>


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

10-10 01:53