0

I created a test.c for testing purpose.

My problem is that I created a pointer at the bottom Main.

Main will call load() to read input from a dictionary and insert the words in a inputfile into a TRIE *dict by calling insert() and getnode(). When I turn on debug mode, the value did change in the load() function. However, after load() return true, the *dict lost all of the value and I cannot get what I expected(i.e. showing cat is present as it is in my dictionary input file).

I have read from other websites that pointers can retain its value after doing malloc. So I have malloced for the *dict. Please kindly let me know why the value disappeared. Thanks a lot!

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#define CHAR_TO_INDEX(c) ((int)c - (int)'a')
#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])

struct dict
{
    char words[46];
    struct dict* dictPath[26];
    bool isEndOfWord;
};


// Returns new trie node (initialized to NULLs)
struct dict *getNode(void)
{

    struct dict *pNode = NULL;
    pNode = (struct dict *)malloc(sizeof(struct dict));

    if (pNode)
    {
        int i;

        for (i = 0; i < 26; i++)
            pNode->dictPath[i] = NULL;
    }

    return pNode;
}

void insert(struct dict *root, const char *key)
{
    int level;
    int length = strlen(key);
    int index;

    struct dict *pCrawl = root;

    for (level = 0; level < length; level++)
    {
        index = CHAR_TO_INDEX(key[level]);
        if (!pCrawl->dictPath[index])
            pCrawl->dictPath[index] = getNode();
        printf("%i\n",index);
        pCrawl = pCrawl->dictPath[index];
    }

    // mark last node as leaf
    pCrawl->isEndOfWord = true;
}

// Returns true if key presents in trie, else false
bool search(struct dict *root, const char *key)
{
    int level;
    int length = strlen(key);
    int index;
    struct dict *pCrawl = root;

    for (level = 0; level < length; level++)
    {
        index = CHAR_TO_INDEX(key[level]);

        if (!pCrawl->dictPath[index])
        {
            return false;
        }

        pCrawl = pCrawl->dictPath[index];
    }

    return (pCrawl != NULL && pCrawl->isEndOfWord);
}

bool load(struct dict *root, char *inputfile){

    // open the dictionary file
    FILE *infile = fopen(inputfile,"r");
    int dictchar;
    char tmpword[46];
    int cnt = 0;

    root = getNode();

    // start iterating to read char
    do
    {
        // read the character
        dictchar = fgetc(infile);
        if (dictchar != '\n')
        {
            // assign the dictionary character to a tmpword. tmpword will be used to fit into TRIES later
            tmpword[cnt] = dictchar;
            cnt ++;
        }
        // if the character is '\n', fit tmpword into TRIES
        else
        {
            tmpword[cnt] = '\0';
            cnt = 0;
            insert(root, tmpword);
        }
    } while (dictchar != EOF);


    return true;
}

int main (int argc, char *argv[]){
    if (argc != 2)
    {
        return 1;
    }
    struct dict *root = malloc(sizeof(struct dict));
    load(root, argv[1]);
    char output[][32] = {"Not present in trie", "Present in trie"};
    printf("%s --- %s\n", "cat", output[search(root, "cat")] );
    return 0;

}

1 Answer 1

0

Whenever you pass a variable in C, you are passing its value. Which means your load's root parameter is a copy of main's root. So changes in the function are not represented in the calling code. Only way around would be using pointers to the value you want to change.

As far as speller is concerned, you may not alter the function signatures. So you would have to pass the root node pointer in another way, store it as a global variable (declared outside of any function).

And please check the possible output of your CHAR_TO_INDEX. Especially for uppercase 'A' to 'Z' and apostrophe '\''.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .