0

I am working on SPELLER and am stuck on the load function. I have no errors the way it's written but the problem is in this code's location.

            typedef struct node
            {
            char word[LENGTH + 1];
            struct node *next;
            }
            node;

            node *alphaArray[26] = {NULL};//I need this to only happen once.  How to make this happen

I need to only call both once and then use both in the insert function over and over as I continue to call insert to load the dictionary's words into a hash table (alphaArray). I've tried passing alphaArray to the function and I've also tried defining it and neither seem to work.

Here is the complete code:

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

    #define LENGTH 45

    //Prototypes
    bool load(const char *dictionary);
    int findPositionInAlphabet(const char *buffer);
    void insert(int key, const char *buffer);
    //Prototypes

    int main(void){

    char *dictionary = "dictionaries/large";

    load(dictionary);

    }//End main function

    bool load(const char *dictionary){


    FILE  *dFile = fopen(dictionary, "r");

        if(dFile == NULL){
            return false;
        }else{
            char *word = " ";

            while(fscanf(dFile, "%s", word) != EOF){

                int positionInAlphabet = findPositionInAlphabet(word);
                insert(positionInAlphabet, word);
        }
        }
        return true;
    }//End load function

    int findPositionInAlphabet(const char *buffer){

    return tolower(buffer[0]) - 'a';
    }//End findPositionInAlphabet function


    void insert(int key, const char *buffer){

            typedef struct node
            {
            char word[LENGTH + 1];
            struct node *next;
            }
            node;

            node *alphaArray[26] = {NULL};//I need this to only happen once.  How to make this happen

    node *newptr = malloc(sizeof(node));

    if (newptr == NULL){ return; }

    strcpy(newptr->word, buffer);
    newptr->next = NULL;

    if (alphaArray[key] == NULL){ alphaArray[key] = newptr;
        }else{ node *predptr = alphaArray[key];

        while (true){

            if (predptr->next == NULL){
                predptr->next = newptr;
                break;
                }
            predptr = predptr->next;
        }
    }
    }//End insert function

1 Answer 1

1

Hmmm..... I have some questions for you, in no particular order, but they'll give you something to think about.

Why is there a main() in dictionary.c? The provided code is set up so that speller can be compiled directly as you add the needed code in each part. But, for argument, I'll just assume that you want to compile and test dictionary.c directly.

You're trying to create alpahArray[] as a local variable inside the insert() function. alphaArray[] appears to contain the head nodes for all of the linked lists in the tree. If you create it locally though, the array will cease to exist every time the code exits insert() and the work will be lost, not to mention creating a big memory leak.

The code may be compiling and "working",i.e., not generating seg faults or other runtime errors, but I suspect that it isn't actually doing what you think. (try printing the tree out in main.)

Finally, if you create alphaArray as a local array var, how will the code access it in check() when it tries to validate a word?

Global vars should be avoided as much as possible, especially when a local will do, but this is a case where globals are justified, if not needed. In this case, the definition of a node struct and the creation of the alphaArray should be global because they are used throughout all of the function calls. It may indeed be possible to implement this pset completely with local vars, it is tremendously more work to pass the array around as a local var and pass by copy than to create a global. When a var is so widely used, it is better to use the global because it is far simpler. Passing it around by copy complicates the code and introduces too much opportunity for error. The risk/reward analysis on this says to use a global.

The struct node definition should also be defined "globally". Ordinarily, it would be placed more appropriately in the .h header file. In this case, it should be in the dictionary.c file because only the dictionary.c file will be uploaded to check50. Consider it a necessary quirk of our learning environment.

If this answers your questions, or gives you what you need to think about, please click the check mark to accept. Let's keep up on forum maintenance. ;-)

[EDIT:addendum] To declare a var as global, it merely has to be declared outside of main and outside of any other function, usually at the top of a file after the #includes and any #define statements. The code to declare is the same as for a local.

Declaring a struct is also done that way.

Note: Declaring and initializing are two different things. At declaration, a global var can only be initialized in the most basic of ways. It MUST be something that can be done at compile time and cannot depend on another var or on anything that depends on a run-time entity. For example, int foo = 0; would be valid, but int foo = bar; would fail.

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

#define LENGTH 45

typedef struct node
{
char word[LENGTH + 1];
struct node *next;
} node;

node *alphaArray[26] = {NULL};

// everything else follows

The best practice is to declare as a global, perhaps initialize as a constant, but initialize with runtime data at first use inside main or inside a function.

For more information, please review any class material on global vars.

4
  • I am trying to test outside of dictionary.c and that's why the main function.
    – ndjustin20
    Commented Mar 9, 2018 at 1:33
  • Also, the rest of your explanation just explains my problem which I don't know how to solve. My question above is stated as so. I need to know how to define a global struct and global instance of a struct. I already know and completely understand the problem I just need to know how to solve the problem as I've already tried a few different options and couldn't get it to work. Can you tell me how to define a global array node *alphaArray[26] = {NULL} and how I define a global struct??
    – ndjustin20
    Commented Mar 9, 2018 at 1:51
  • see edit to my answer.
    – Cliff B
    Commented Mar 9, 2018 at 2:08
  • Thank you @Cliff B for your edited answer!!
    – ndjustin20
    Commented Mar 9, 2018 at 15:55

You must log in to answer this question.

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