Home > Software design >  CS50 Pset 5 (speller): Initialization of variable as char or char*?
CS50 Pset 5 (speller): Initialization of variable as char or char*?

Time:03-01

I've completed CS50 PS5 (speller). I understand the program except for one line of code in the load function. The syntax seems trivial but I'd like to understand what's going on.

bool load(const char *dictionary)
{
    // Open dictionary
    FILE *d_pointer = fopen(dictionary, "r");
    if (d_pointer == NULL)
    {
        printf("Unable to open %s\n.", dictionary);
        return false;
    }

    // Variable for scanned words
    char w_scan[LENGTH   1];

    // Scan words from dictionary
    while (fscanf(d_pointer, "%s", w_scan) != EOF)
    {

        // Create new node
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            return false;
        }

        // Copy word into node
        strcpy(n->word, w_scan);

        // Hash word
        int h_value = hash(w_scan);

        // Insert node into hash table
        n->next = table[h_value];
        table[h_value] = n;
        d_size  ;
    }

    // Close dictionary
    fclose(d_pointer);

    return true;
}

My questions is, why does the variable that represents scanned words (w_scan) have to be initialized as a char and not a string? If I replace the initialization of w_scan as char *w_scan instead of char w_scan I get the following error:

dictionary.c:83:36: error: format specifies type 'char *' but the argument has type 'char **' [-Werror,-Wformat] while (fscanf(d_pointer, "%s", w_scan) != EOF)

Sorry for the basic question. I'm obviously missing something here but I'm scratching my head as to what I'm not understanding.

CodePudding user response:

My questions is, why does the variable that represents scanned words (w_scan) have to be initialized as a char and not a string?

It is not being initialized as a char. It is being initialized as an array of char, which is exactly the type of object that holds a string. Which brings me to a second important point: data type char * does not represent a string. A string is a sequence of one or more chars, up to and including a terminator with value 0. Objects of type char * are useful for referring to strings, but they must not be confused with the strings themselves. This is in the category of distinguishing between the pointer and the thing to which it points.

If you instead write

    char *w_scan[LENGTH   1];

then you thereby declare an array of pointers, not an array of char. That might in some cases be exactly what you want, but this is not one of those cases.

CodePudding user response:

In this declaration

char w_scan[LENGTH   1];

there is declared an array of LENGTH 1 characters that is used to read words from a file in a loop by means of this call

fscanf(d_pointer, "%s", w_scan)

This declaration

char * w_scan[LENGTH   1];

does not make a sense. The function reads sequences of characters not sequences of pointers.

  • Related