1

One clip of my code:

#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>    
#include <string.h>
#include <stdio.h>
#include "dictionary.h"

// Define a struct node to store words
typedef struct node
{
    bool is_word;
    struct node* children[27];
}
node;

// Set the root of tries
node root = {false, {NULL}};

Another clip of my code:

int word_counter = 0;
bool load(const char* dictionary)
{
    // Read words from dictionary and store them in the tries
        // Read the first letter
        // Store it in the relevant children array (node -> children[x])
        // Read the next letter
        // Store it in the relevant children array (node -> children[x] -> children[x])
        // If a space is read, set is_word true. And contine the loop.  

    // Open the dictionary
    FILE* infile = fopen(dictionary, "r");
    if (infile == NULL)
    {
        printf("Could not open %s.\n", dictionary);
        return false;
    }

    node* current_node = &root;
    for (int c = fgetc(infile); c != EOF; c = fgetc(infile))
    {
        if (c != '\n') 
        {
            // Calculate the index of the children array if c is a letter or an apostrophe.
            int index = (isalpha(c)) ? (c - 'a'): 26;  
            // If children[index] is NULL, add another node to it to suggest that the word hasn't ended yet.
            if (current_node -> children[index] == NULL)
            {
                current_node -> children[index] = malloc(sizeof(node));
                if (current_node -> children[index] == NULL)
                {
                    printf("Memory allocation failed!");
                    return false;// Or return 1???
                }
                current_node = current_node -> children[index];    
            }
            else
            {
                current_node = current_node -> children[index];
            }

        }
        // If c is a '\n', mark the node as the end of the word. And reset the current_node to the root.
        else
        {
            current_node -> is_word = true;
            word_counter ++;
            current_node = &root;
        }
    }

    fclose(infile);
    return true;
}

My questions:

1.

current_node -> children[index] = malloc(sizeof(node));

current_node -> children[index] = {false, {NULL}}

After allocating memory for a new node, I tried to set the node to NULL, but my code reported error.

But the following lines work fine.

2.

node root = {false, {NULL}}

3.

current_node -> children[index] = calloc(1, sizeof(node));

Code 2 create a node type in memory. How about the code 1? Does it also create a node type or merely allocate the memory of the size of a node? How about the code 3? Does it also create a node type or merely allocate the memory of the size of a node and set the data to zeroes? If code 1 and 3 merely allocate memory, what operators should I use to assign values as current_node -> children[index] = {false, {NULL}} or current_node -> is_word = truewon't work?

You must log in to answer this question.

Browse other questions tagged .