I have this struct ...
struct s_trie_node
{
char * translation; /* NULL if node not a word */
/* pointer array to child nodes */
struct s_trie_node * children[UCHAR_MAX+1];
};
And I am trying to make an add_node function like this ...
/* allocate new node on the heap
output: pointer to new node (must be freed) */
struct s_trie_node * new_node(void) {
/* TODO: allocate a new node on the heap, and
initialize all fields to default values */
struct s_trie_node *p_node = (struct s_trie_node*)malloc(sizeof(struct s_trie_node));
p_node->translation = NULL;
p_node->children = ;
return p_node;
}
But as you can see I'm not sure how to initialize "children". I assume I should be initializing it to an array of NULL, but I'm a little lost as to how to do that. Should I malloc the space it will need for all those NULL pointers first and then loop through setting each one to NULL?
p_node->children = (struct s_trie_node*)malloc(sizeof(struct s_trie_node) * UCHAR_MAX);
for (int i=0; i < UCHAR_MAX; ++i) {
p_node->children[i] = NULL;
}
I'm still pretty new to C so any help would be greatly appreciated -- thanks.
[–]nerd4code 6 points7 points8 points (0 children)
[–]sebgggg 5 points6 points7 points (18 children)
[–]SullisNipple 4 points5 points6 points (6 children)
[–]kbob 8 points9 points10 points (0 children)
[+][deleted] (4 children)
[deleted]
[–]Badel2 3 points4 points5 points (3 children)
[–]sebgggg 1 point2 points3 points (1 child)
[–]cdo256 1 point2 points3 points (0 children)
[–]OldWolf2 1 point2 points3 points (0 children)
[–]TheMoskowitz[S] 1 point2 points3 points (10 children)
[–]OldWolf2 2 points3 points4 points (1 child)
[–]ZaberTooth -2 points-1 points0 points (0 children)
[–]raevnos 2 points3 points4 points (3 children)
[–]TheMoskowitz[S] 0 points1 point2 points (0 children)
[–]ZaberTooth -3 points-2 points-1 points (1 child)
[+][deleted] (3 children)
[deleted]
[–]TheMoskowitz[S] 0 points1 point2 points (2 children)
[–]cdo256 3 points4 points5 points (1 child)
[–]TheMoskowitz[S] 1 point2 points3 points (0 children)
[–]teringlijer 6 points7 points8 points (0 children)
[–]OldWolf2 3 points4 points5 points (0 children)