Search code examples
cfunctionpointersstructtrie

Modifying a C struct's children with a function (for the application of a trie)


I'm trying to create a trie in C, to load a dictionary into a trie, right now, but I am having trouble with modifying the struct.

I have a function that produces a blank node, right now, and that seems to be working fine. The main function scans through the dictionary file, and calls an "insert" function, as follows:

while (ftell(inptr) < endIndex)
{
    fscanf(inptr,"%[^\n]", word);
    printf("%s\n", word);

    //main insert function
    insert(word, root);


    //skips past this word
    fseek(inptr, 1, SEEK_CUR);
}

This is immediately proceeded by a test in which I try to determine the value of a boolean in the "A" value of the trie.

printf("%i\n", (int)(root -> ptr[0] -> isEnd));

But that yields an error, because root's pointer is apparently null (member access within null pointer of type 'struct node').

The implementation of the function insert is as follows.

bool insert(char *word, struct node *root)
{
    if (strcmp(word, "") == 0)
    {
        for (int i = 0, size = 27; i < size; i++)
        {
            root -> ptr[i] = NULL;
        }
        root -> isEnd = true;
        return false;
    }
    else
    {
        //gets the first letter of the word
        char firstLetter = word[0];

        //figures out which place in the array it goes
        int firstLetterInt;
        if (isalpha(firstLetter))
        {
            firstLetterInt = firstLetter - 'a';
        }
        else
        {
            firstLetterInt = 26;
        }

        //gets the rest of the word
        char *subbuff = calloc(45,1);;
        memcpy(subbuff, &word[1], strlen(word) - 1);
        subbuff[strlen(word)] = '\0';

        if(!(root -> ptr[firstLetterInt]))
        {
            root -> ptr[firstLetterInt] = blankNode();
        }

        insert(subbuff, root -> ptr[firstLetterInt]);
        free(subbuff);
        return true;
    }
}

I understand that I should be using pointers and the like, but I've played around with that and I can't seem to get it to work. I even tried to pass the nodes in as struct node **root, but the same issue seemed to appear. Another strange thing is that during the while loop, they seem to recognize the insertions, because when "caterpillar" is inserted, the "c", "a", and "t" are not created as new nodes.


Solution

  • if you dont initialize the structure node the insert is not going to do that for you with magic, the problem of your code (at least the snippet you post) is you never create any instance of struct node so root is and going to be forever null,
    Here is a demo code shows you how to assign struct fields in a function

    #include <stdio.h>
    /***struct defenition***/
    typedef struct demo_t
    {
        int foo;
        int bar;
    }demo_t;
    /***Function to change values***/
    void insert(demo_t *demo)
    {
        demo->foo = 1;
        demo->bar = 2;
    }
    /***main***/
    int main()
    {
        //create instance of struct
        demo_t demo;
        demo.foo = 0;
        demo.bar = 0;
        //pass the reference
        insert(&demo);
        //verify
        printf("foo = %d    bar = %d\r\n", demo.foo, demo.bar);
    
        return 0;
    }