F
fix
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 = 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.
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 = 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.