Home > Software design >  Why does the struct remain NULL even when assigned to another struct of the same type?
Why does the struct remain NULL even when assigned to another struct of the same type?

Time:10-24

I am attempting to generate a binary tree of strings. The structs contained in the list are created with this code:

typedef struct WordNode {
    char *word;
    unsigned count;
    struct WordNode *left;
    struct WordNode *right;
} wordnode;

I begin in the main function by first initializing the root node to NULL, like so:

// The initial struct begins empty.
wordnode *head = NULL;

This is because the initial struct will be the first word contained within a given input file. Once it is initialized to NULL, I begin processing the file contents:

// While the end of file marker has not been reached...
while (EOF != fscanf(inHandle, BUFFMT, wordBuffer)) {
    // Clean the word currently in the wordBuffer.
    cleanWord(wordBuffer);
    
    // If the buffer contains an actual word...
    if (strlen(wordBuffer) > 0) {
        
        // Add one to the total word count.
        totalWords  ;

        placeWordInTree(wordBuffer, head);
        
    }
}

The placeWordInTree function processes each word of the input file (accounting for the first empty root node):

void placeWordInTree(char *word, wordnode *currNode) {
    // If the current node is empty, initialize it to a newly generated node.
    if (currNode == NULL) {
        currNode = generateNode(word);
        
    // If the base case has not been satisfied...   
    } else {
        // Store the result of comparing the given word and the current structs word.
        int strComp = strcmp(word, currNode->word);
    
        // If the words are equal, add one to the current structs counter.
        if (strComp == 0) {
            currNode->count  ;
        
        // If the given word is less than the struct word, recursively call this function
        // with the given node and the current structs left node.   
        } else if (strComp < 0) {
            placeWordInTree(word, currNode->left);
        
        // If the given word is greater than the struct word, recursively call this
        // function with the given word and the current structs right node.
        } else if (strComp > 0) {
            placeWordInTree(word, currNode->right);
        }
    }
}

This function checks if the current node is NULL. If it is, it assigns a new struct generated by the generateNode function using the given word to the current struct. If the current struct is not null, it compares the given word to the current structs word and either adds one to the counter or attempts to recursively place the node in the current nodes left struct (if it is less than the current structs word) or the right struct (if it is more).

The generateNode function generates a node like this:

wordnode *generateNode(char *word) {
    // Allocates enough memory to hold a new word node.
    wordnode *newNode = malloc(sizeof(wordnode));
    
    // Allocates enough memory to store the new nodes given word.
    newNode->word = malloc(strlen(word) 1);
    
    // Copies the given word into the new nodes word field.
    strncpy(newNode->word, word, strlen(word));
    
    // Sets the new nodes count to 1, and its branches to NULL.
    newNode->count = 1;
    newNode->left = NULL;
    newNode->right = NULL;
    
    // Return the new node.
    return newNode;
}

The issue I am experiencing is that the program seems to believe that the head struct is always NULL when it is checked in the placeWordInTree function. When placing printf statements for debugging, which told me when each of the possible outcomes of the placeWordInTree function, it always executed the code as if the head struct was always NULL. What is even more confusing though, is that when the contents of the tree were printed, the word contained by the head struct was always the first word of the input file. After lengthy debugging with no success, the tree is not even printed anymore.

I've checked the file input, and it works fine. I've checked the display function, and it worked fine (but I'll put it below here just in case.) And I've checked the main function, which all looked fine. The issue just seems to be that the else statement for the placeWordInTree functions initial if statement never executes.

unsigned displayTree(wordnode *currNode) {
    unsigned treeLength = 0;
    
    // If the current node is NULL, return 0;
    if (currNode == NULL) {
        return treeLength;
        
    // If the current node is not null, recursively call displayList on its left and
    // right pointers and add their return value to the total tree node count.  
    } else {
        printf("\n%6u\t%s", currNode->count, currNode->word);
        treeLength  = displayTree(currNode->left);
        treeLength  = displayTree(currNode->right);
    }
    
    return treeLength  ;
}

CodePudding user response:

Yo need to pass the node by reference in the placeWordInTree function. In C that is achieved by passing a pointer to the desired variable to change.

void placeWordInTree(char *word, wordnode **currNode)

And when invoking the function add the address of operator.

placeWordInTree(wordBuffer, &head);
  • Related