0

I'm building a hash table in C. Everything seems to work fine except this variable, cur_item, that report me an error at compilation.

Here is the code :

void insert(hash_table* ht, const char* key, const char* value) {            
   const int load = ht->count * 100 / ht->size;                               
   if (load > 70) {                                                           
     resize_up(ht);                                                           
   }                                                                          
   item* item = new_item(key, value);                                         
   int index = get_hash(item->key, ht->size, 0);                              
   item* cur_item = ht->items[index];                                         
   int i = 1;                                                                 
   while (cur_item != NULL) { 
     /** PROCESS **/
   }
}

Here is my Makefile (not the best one I guess):

main: main.o hash_table.o prime.o                                            
          gcc  -g -Wall -lm -o main.out ./build/main.o ./build/hash_table.o ./\
   build/prime.o                                                                

main.o: ./src/main.c ./src/hash_table.h                                      
          gcc -c  ./src/main.c -o ./build/main.o                               

hash_table.o: ./src/hash_table.c ./src/hash_table.h                          
         gcc -c  ./src/hash_table.c -o ./build/hash_table.o                   

prime.o: ./src/prime.c ./src/prime.h                                         
         gcc -c  ./src/prime.c -o ./build/prime.o    

And here is the error :

./src/hash_table.c: In function ‘insert’:
./src/hash_table.c:65:9: error: ‘cur_item’ undeclared (first use in this function); did you mean ‘del_item’?
   item* cur_item = ht->items[index];
         ^~~~~~~~
         del_item
./src/hash_table.c:65:9: note: each undeclared identifier is reported only once for each function it appears in
make: *** [Makefile:10: hash_table.o] Error 1

The type item represent a structure I created. del_item is a function, no reason to use it there.

If I declare cur_item before the if loop and then initialize its value after the get_hash() function, the compilation works fine.

Can someone explain me why the first compilation failed ? Is there something missing in my Makefile ?

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
Cerclique
  • 61
  • 1
  • 6
  • 3
    `item* item` didn't make the compiler unhappy, so why did `item* cur_item`? Probably you don't show us everything we need... – gsamaras Sep 18 '17 at 08:18
  • 1
    Also, variables should be initialized on top before any execute statement. Even if you are in latest C standard, this should be consider otherwise code is ugly and noisy. #justSayin – unalignedmemoryaccess Sep 18 '17 at 08:21
  • I'm willing to give you more information but I can't find relevant one to help you. I've declared other `item` variable in my source code and this one is the only one who broke – Cerclique Sep 18 '17 at 08:26
  • try to compile with the option -std=gnu99 – elhadi dp ıpɐɥןǝ Sep 18 '17 at 08:26
  • 4
    @tilz0R some coding styles want it like this, others don't. I consider it good style to introduce variables at their first use. Don't impose medieval requirements to newcomers. – Jens Gustedt Sep 18 '17 at 08:37

1 Answers1

4

You just overloaded the identifier item to be a variable. So then the compiler is confused and does not know what you want when you try to declare cur_item.

Don't use the same name for a variable and a type. Obviously this confuses you and the compiler.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • Sorry for the late answer but I couldn't test earlier. You were right, that solved my problem. That wasn't smart of me to name my variable exactly the same as the type of my structure. Thanks ! – Cerclique Sep 22 '17 at 12:28