0

enter image description hereHaving a painfully hard time trying to understand all of this linked list stuff and exactly where my memory is being allocated too. This is something I never mastered in the past. I'm having a compiling error as the following ", clang-7: error: linker command failed with exit code 1 (use -v to see invocation)." Really don't how to even decipher that. Also, I believe there are a few other issues in my code, if any could help as best you could. I really need guided help here. So much to take it all at once. Thanks a ton!

// Implements a dictionary's functionality

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

// Represents number of children for each node in a trie
#define N 27

//to keep track of number of words in the dictionary
//index varaible for the words
int dictionNum = 0, indexWord = 0;

// Represents a node in a trie
typedef struct node
{
    bool is_word;
    struct node *letters[N];
}
node;

// Represents a trie//topmost node
node *root = NULL;


// Returns true if word is in dictionary else false
bool check(const char *word)
{
    node *t = root;
// to check each letter in the input word
    for(int i = 0; i < strlen(word); i++){

        //creae index for letters in the trie
        if(isalpha(word[i])){
            //the index check for letters
            indexWord = tolower(word[i]) - 'a';
        }
        else{
            //the index check for apostraphe
            indexWord = 26;
        }
        //if the element in the alphabet in the trie is NULL
        if(t->letters[indexWord]==NULL){

            return false;
        }
        else{
            //if not NULL, keep going to the next letter
            t = t ->letters[indexWord];
        }

        // check if the input word is true or false
        if(t -> is_word == true){
              return true;
        }
        else{
            return false;
        }

    }

    return false;
}


// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // Initialize trie
    root = malloc(sizeof(node));//if NULL, malloc a new node
    // need a node that will traverse through the trie
    node *t = root;
    //error check
    if (root == NULL)
    {
        return false;
    }
    // initializing it as false, since dictionary isn't loaded yet
    root->is_word = false;

    for (int i = 0; i < N; i++)//for every dictionary word, iterate through the trie
    {
        //follow a pointer to get a variable in a struct
        //foo->bar is the same as (foo*).bar
        //it gets the member bar from the struct that foo points ot
        //setting all nodes to NULL initially
        root->letters[i] = NULL;
    }

    // Open dictionary
    FILE *file_ptr = fopen(dictionary, "r");
    if (file_ptr == NULL)
    {
        fprintf(stderr, "File does not exist\n");
        unload();
        return false;
    }


    // Buffer for a word
    char word[LENGTH + 1];

    // Insert words into trie
    while (fscanf(file_ptr, "%s", word) != EOF)
    {
        //iterate over the letters of the word in order to create an index
        for(int i = 0, n = strlen(word); i<n ; i++){
            //point traversal pointer to where root is pointing
            node *t = root;
            if(isalpha(word[i])){
                indexWord = tolower(word[i]) - 'a';
            }
            else{
                indexWord = 26;
            }

            //check the value
            if(t->letters[indexWord] == NULL){
                //have the traversal pointer point to the new node
                t->letters[indexWord] = calloc(1, sizeof(node));
            }
            else{
                t = t->letters[indexWord];
            }

            //at the end of the line you have hit the end of a word
            //have to round it out so it can go back to the root
            t -> is_word = true;
            t = root;
            dictionNum ++; //increment for every word in the dictionary
        }
    }

    // Close dictionary
    fclose(file_ptr);

    // Indicate success
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    return dictionNum;
}


// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{

    node *ptr = root;
    while(!ptr){
        //temporary Node to store the header of the list
        node *next = ptr;
        free(ptr);
        ptr = next;
    }
    return true;
}
4
  • 1
    Can you please post the full text of the invocation, including the command that you used to compile and everything generated by the command? – Cliff B Aug 15 '19 at 22:17
  • $ make ./speller texts/lalaland.txt clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow speller.c -lcrypt -lcs50 -lm -o speller /tmp/speller-fc7eba.o: In function main': /home/ubuntu/pset4/speller/speller.c:40: undefined reference to load' .... clang-7: error: linker command failed with exit code 1 (use -v to see invocation) <builtin>: recipe for target 'speller' failed make: *** [speller] Error 1 – user24000 Aug 17 '19 at 22:03
  • Sorry, I know it's a lot. – user24000 Aug 17 '19 at 22:03
  • I added the picture. – user24000 Aug 20 '19 at 4:00
0

Looking at the comment with the command that you issued, I'd say that you're having some issues with how you're invoking make and where you have placed all the files.

The command to build the compiled program is make speller. No need for the ./ or the runtime parameters like the filename of a text file.

Next, it looks like the compiler cannot find the file dictionary.c. Is it in the same directory as speller.c, as well as the .h files? Are you also running make from the same directory? The error message says that it can't find the load function, which should be in dictionary.c and in the same directory as everything else.

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

5
  • No, it didn't everything is where it is suppose to be. – user24000 Aug 20 '19 at 2:17
  • OK, can you please recreate this problem and take a screenshot of the fullscreen IDE window please? Edit the question and post the screenshot there. – Cliff B Aug 20 '19 at 2:32
  • I did what you ask to the edit. – user24000 Aug 20 '19 at 8:38
  • based on what I see in the photo, you're in the wrong directory. You are in ~/pset4/speller/texts but the code files are one directory up, in ~/pset4/speller Move to the correct directory and try again. If it still fails, post another screen capture. – Cliff B Aug 20 '19 at 8:57
  • Thank you, you're right. Yeah I am just having major issues with this program. – user24000 Aug 21 '19 at 2:03

You must log in to answer this question.