0

I think this relates to my understanding (or lack of) of pointers. I have 3 errors for the following code -- this is just inside my while statement. Both root and current are defined as nodes and malloced:

int index = 0;
// defines constant to use in calculating index
const int ltr = 'a';
root = NULL;
current = NULL;

// checks each character in word one by one
for (int i =0; i < strlen(word); i++)
{
        // ' is at index 26
        if (word[i] == '\'')
        {
            index = 26;
        }
        // determines index number of letters in words based on lower case ascii mod with base
        else
        {
            index = word[i] % ltr;
        }
        // if not yet any letters checked
        if (root == NULL)
        {
            // allocate space for node
            children[index] = malloc(sizeof(node));
            // root now has child pointer -- index corresponds to alphabet
            root->children[index];
            // the current node is now equivalent to the current node
            current = children[index];
        }
        // if already started (ie Root now points to something other than NULL) and
        // there is already something at child[index]
        else if (children[index] != NULL)
        {
            // current node holds the pointer at child[index]
            current->children[index];
            // and the child[index] pointer gets assigned to current
            // child[index] currently points potentially to NULL
            current = children[index];
        }
        // but if child[index] does not yet contain pointer to next node
        // need to malloc space
        else
        {
            children[index] = malloc(sizeof(node));
            current->children[index];
            current = children[index];
        }
    }
    // end of word reached
    current->is_word = true;

And here are the three errors I get (I know I don't have the line numbers above, but hope it makes sense):

dictionary.c:78:37: error: expression result unused [-Werror,-Wunused-value]

           root->children[index];
           ~~~~~~~~~~~~~~ ~~~~~^

dictionary.c:87:40: error: expression result unused [-Werror,-Wunused-value]

           current->children[index];
           ~~~~~~~~~~~~~~~~~ ~~~~~^

dictionary.c:97:40: error: expression result unused [-Werror,-Wunused-value]

          current->children[index];
            ~~~~~~~~~~~~~~~~~ ~~~~~^
0
        current->children[index];

This is essentially the name of a variable. It's equivalent to writing a line of code that only contains a single var or array element and does nothing with it. It would be the same as writing the following code:

int var1;
var1;

float array[10];
array[3];

As each of these examples shows, nothing is being done with the vars. Your code is doing the same kind of thing. Are you trying to assign something to it, or use it to assign it's content to something else?

I suspect that you're trying to do something like this:

    if (root == NULL)
    {
        // allocate space for node
        root = malloc(sizeof(node));
        // root now has child pointer -- index corresponds to alphabet
        root->children[index] = NULL;
        // the current node is now equivalent to the current node
        current = root;
    }

My code is flawed (I don't know some of your var contexts), but it demonstrates the proper use and creation of a structure and assignment to a pointer. Note also, when you create a structure using malloc, you should initialize ALL of the elements of that struct, like the children[] array. You might also want to study the calloc() function.

THis should get you started again.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

4
  • I will try this and come back, but in the meantime, how to initialize is_word and an array of null pointers? Thanks.
    – KitSeason
    Jun 23 '17 at 18:30
  • The load function now compiles, but won't know for sure if it's working of course until I do the check function. But on running I got a segmentation fault -- is this due to load function or that I don't have the other functions working?
    – KitSeason
    Jun 23 '17 at 19:05
  • It's almost certainly a problem in your code. The other functions will simply return false because they are nothing more than code stubs.
    – Cliff B
    Jun 23 '17 at 19:18
  • Fixed it. Thanks. Just needed to go over my code a bit more carefully.
    – KitSeason
    Jun 24 '17 at 18:53

You must log in to answer this question.

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