hannah
hannah

Reputation: 909

Pointers and structs

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

#include "dictionary.h"

#define HASH_SIZE 100

// prototype
int hash(char *word);

// counter
int counter;

// node
typedef struct
{
    char *word;
    node *next;
} node;

// hash table
node *hashtable[HASH_SIZE];

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

    // set all values in the hash table to null
    for(int i = 0; i < HASH_SIZE; i++)
    {
        hashtable[i] = NULL;
    }

    // set the counter to 0
    counter = 0;

    // iterate through the words in the dictionary
    while (!feof(dict))
    {
        // get word into a string
        char gotcha[LENGTH];
        fscanf(dict, "%s", gotcha);

        // declare a node and allocate memory
        node n;
        n.word = malloc( strlen(gotcha)*sizeof(char) );

        // save the word into the node
        strcpy(n.word, gotcha);

        // hash the word, baby!
        int hash_value = hash(n.word);

        // start saving addresses to the hashtable
        n.next = hashtable[hash_value];
        hashtable[hash_value] = &n;

        //test
        int len = strlen(n.word);
        printf("%s\n", n.word);
        printf("%i\n", len);

        // that's one more!
        counter++;
    }


    fclose(dict);

    return true;
}

I am receiving the following two errors on these two lines of code:

    n.next = hashtable[hash_value];
    hashtable[hash_value] = &n;

dictionary.c:89:16: error: assignment from incompatible pointer type [-Werror] dictionary.c:90:31: error: assignment from incompatible pointer type [-Werror] How do I save pointer values in these two places? I am new to this, so please bear that in mind. :)

Upvotes: 1

Views: 284

Answers (3)

Jim Balter
Jim Balter

Reputation: 16406

Define typedef names of structs before defining the structs. This allows mutually referring structs without concern for order and doesn't require inconsistent definitions, sometimes with the struct keyword and sometimes without it. Note that in C++ you can do away with the typedef line entirely:

typedef struct node node;

struct node
{
    char* word;
    node* next;
};

Upvotes: 0

Alan Curry
Alan Curry

Reputation: 14711

This:

typedef struct
{
    char *word;
    node *next;
} node;

is a syntax error. The node *next; occurs before the end of the typedef that creates node as a type. If your compiler for some reason accepted this, it probably thinks there are 2 different types called "node" now, which explains why one of them isn't compaible with the other. You should give up on that typedef silliness (structs generally shouldn't be typedef'ed) and just use

struct node
{
    char *word;
    struct node *next;
};

Upvotes: 0

perreal
perreal

Reputation: 97918

In your structure, the type node is not yet defined. Change it to use the structure tag:

typedef struct node
{
    char *word;
    struct node *next;
} node;

Upvotes: 2

Related Questions