-2

I have this exercise for homework that follows:

Consider the following definition to represent dynamic hashtables with collision treatment chaining.

typedef struct entry{
   char key[10];
   void *info;
   struct entry *next;
}   *Entry;

typedef struct hashT{
   int hashsize;
   Entry *table;
}   *HashTable;

Define `HashTable newTable(int hashsize) note that the memmory necessary must be allocated and that all table entrys must be initialized with empty list.

My proposition is this:

HashTable newTable(int hashSize){
   Entry *table = malloc(sizeof((struct entry)*hashSize));
   HashTable *h = malloc(sizeof(struct hashT));
   h->hashSize = hashSize;
   h->table = table;
   return h;
}

im pretty sure the logic is correct. my problem is with pointers. for example, sometime I see (char *), or in this case (table *) before the malloc function... Is this necessary?

and for the return, should I return h, or *h? and whats the difference?

thank you

alterEgg
  • 11
  • 1
  • Try compiling with warnings enabled. You should be seeing errors. Hiding pointers in typedefs is usually bad for the exact reasons seen here. For example `Entry *table` is a pointer to pointer to struct entry, yet you try to allocate space for `sizeof (struct entry) * hashSize`, though that too is misparenthesized. – Ilja Everilä Dec 30 '17 at 15:17
  • 2
    This is C basics. What does you C book say about pointers and structures? What is not clear about its explanation? Did you check another textbook/resource? Ask you teacher? And never ever `typedef` a pointer to data types, it is worst practice and causes bad style! – too honest for this site Dec 30 '17 at 16:05

1 Answers1

0

Firstly,

Entry *table = malloc(sizeof((struct entry)*hashSize));

will be

Entry table = malloc(sizeof(struct entry)*hashSize);
                           ^^^    
                           Look at the parentheses here.

Also you will do the same change over here,

HashTable h = malloc(sizeof(struct hashT));

Same goes with HashTable. You forgot that you have hidden the pointers inside typedef which you shouldn't.

With the above mentioned changes the code will be

HashTable newTable(int hashSize){
   Entry table = malloc(sizeof(struct entry)*hashSize);
   HashTable h = malloc(sizeof(struct hashT));
   h->hashSize = hashSize;
   h->table = table;
   return h;
}

And Dont hide pointer behind typedef.(Wish I could write this in red color but yes this will save you from many many problems).

What is the type of Entry*?

It is of type struct entry **. Here in your case you don't need it while allocating. It will be an overkill if you do so.

What is the difference between h and *h?

Well typewise h is of type struct hashT** and so *h will be of type struct hashT*.

What shall you do next?

  • Compile all your code with warnings enabled. gcc -Wall -Werror progname.c
  • Check the return value of malloc.
  • Free dynamically allocated memory when done working with it.
  • Read How to debug small programs.
  • Grab a book.
Community
  • 1
  • 1
user2736738
  • 30,591
  • 5
  • 42
  • 56