1

I'm attempting to create a dictionary using a hash-table, and so I've created a structure called node that has a word and a next pointer associated with it:

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// Hash table
struct node *table[5];

In main, I've initialised a node, and am now attempting to load it into the hash-table:

void hash_insert_node(struct node **hash_table, struct node *n, int value)
{
    hash_table[value] = n;
    printf("%s\n", hash_table[value]->word);
}

I have prototype for this function in a file named dictionaries.h, and this code in a file named dictionaries.c. At the top of dictionaries.c, I have

#include "dictionaries.h"

If I run the code now, I get the following error:

declaration of 'struct node' will not be visible outside of this function 
[-Werror,-Wvisibility]

The only way I've found to resolve this is to move the definition of the structure to dictionaries.h, but that seems silly.

This may be a trivial question, but I'd greatly appreciate any help.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
Dan Jackson
  • 185
  • 1
  • 9
  • 2
    You can merely add "struct node;" in "dictionnary.h" before the declaration of hash_insert_node() to tell the compiler that it is a forward declaration. cf. https://en.cppreference.com/w/c/language/struct – Rachid K. Oct 12 '20 at 10:15
  • 1
    What exactly is in `dictionaries.h`. What is in `main`? Post a [mcve]. Don't describe your code, show it instead. – Jabberwocky Oct 12 '20 at 10:15

2 Answers2

2

The only way I've found to resolve this is to move the definition of the structure to dictionaries.h, but that seems silly.

It doesn't seem silly to me, a .h file seems a perfectly good place to place a struct declaration.


The problem arises because your function is not aware of the existance of the struct.

You can solve this in some different ways:

  1. Forward declare the struct before the declaration of the function, i.e.:
 struct node;
 char *GetHashWord(struct node **hash_table, int value);
  1. Place the struct in a separate .h file which you could name, for instance, data_structure.h and #include it in dictionaries.h.
  2. Keep your original fix, I see no reason why that should be considered a bad practice.

On a side note, if you are going to give an alias to your struct you may as well use it:

void hash_insert_node(node **hash_table, node *n, int value){/*...*/}
                      ^^^^               ^^^^
anastaciu
  • 23,467
  • 7
  • 28
  • 53
1

usually if you create a data structure you'd want to make the implementation of it abstract from the user, there is no need for the user to know what variables the struct it self contains. if you want to know the word inside of a hash table you should write a getter function inside the .c file that you defined the structure at, and include the declaration of this function inside the .h file. an example for the function implementation in the .c file.

char *GetHashWord(struct node **hash_table, int value)
{
   return hash_table[value]->word;
}

an example for the decleration:

char *GetHashWord(struct node **hash_table, int value);

that way the user doesn't know how you implemented the struct and you are able to include a level of abstraction and still provide the variables you want him to access if needed.

Gilad
  • 305
  • 1
  • 2
  • 8