# Pset 5 - struggling to understand how to check for NULL in a trie

I'm sure my code has a ton of errors, but I'm mostly on line 61 right now.

I'm getting this error: dictionary.c:61:25: error: invalid operands to binary expression ('struct node' and 'void *') if (trav[c] == NULL)

when I attempt to make. I think there's something wrong with the [c] part of trav[c], but I have no idea how else to test for a specific part of a trie. Can someone explain this to me?

``````    typedef struct node //Creates trie
{
bool is_word;
struct node*child;
}
node;

char s[LENGTH];
if (fgets(s, LENGTH, dicptr) != NULL) //Iterate over words until EOF
{
int slen = strlen(s);

for (int i = 0; i < slen; i++)
{
int c = s[i];
if (c >= 65 && c <= 90) // Cast char from ascii to numeric (A-Z to 0-26)
{
c = c - 65;
}
if (c >= 97 && c <= 122) // Cast char from ascii to numeric (a-z to 0-26)
{
c = c - 97;
}
if (c == 39) // Cast apostrophe to 27
{
c = 27;
}
struct node *trav = root; // Set travel equal to root

if (trav[c] == NULL)
{
trav[c] = malloc(sizeof(node)); // create new node
trav->child[c] = NULL; // create child pointer
trav = child[c]
}
else if (trav->child[c] != NULL) // If node exist, travel
{
trav->child[c];
}
if (i == slen)
{
child[c].is_word = True; // If end of word, mark
}
}
}
``````

``````    struct node *trav = root; // Set travel equal to root

if (trav[c] == NULL)
``````

`trav` is declared as a single pointer to a node. On the very next line, the code tries to use it like an array. Can't do that. Maybe you meant this?

``````    if (trav == NULL)
``````

That should get you past line 61, but more issues do lie in wait. Happy coding! ;-)

If this answers your specific question, please click on the check mark to accept. If you have a new question, please create a new question. Let's keep up on forum maintenance.

• I want trav to initially point to the root. Then, if say, I parse a char "A", to point to "0" and then check if that part is NULL or already has another node inside of it. Would sometihng like ` if (trav->child[c] == NULL)` work? – Coal143 Apr 29 '18 at 4:06
• I'm inclined to say yes, it should. But that's no guarantee there aren't other issues. I also haven't seen your node struct definition, so it's a guess. I'm assuming that inside the struct, `child` is declared as an array of pointers. – Cliff B Apr 29 '18 at 4:25
• I'll update my node struct definition at the top of my OP – Coal143 Apr 29 '18 at 4:27
• Exactly what I expected. So, yes, as I said, including the qualifiers. – Cliff B May 1 '18 at 2:44