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 ?