Hashtable Question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hashtable Question

How come this hashtable can install the first name and the definition in a hashtable? Because the function lookup assigns np = hashtab[hash(s)] and it is a garbage value. After that boolean expression is controlled and np != NULL and after that if condition is also false. So the iteration np = np -> next is assigned but it is also a garbage value so to my knowledge this is an error. But program works well. I do understand the rest of the program and how the hashtable is constructed but I can't understand how is this program able to install the first value. Ex: Name = "IN" , Definition "1" Code : https://code.sololearn.com/cNcon0vEQjig/#c

13th Apr 2020, 4:26 PM
mert aydın
3 Answers
+ 1
First of all, I must say this code is quite hard to read. The formatting is inconsistent and several rather bad practices can be found. And one bad practice is the lack of an initializer, so the values could be random. It seems however that the compiler automatically uses NULL as default value for all pointers so it still works. So actually np is NULL despite you might expect a garbage value. But don't use this code in production, other compilers might handle this differently :)
13th Apr 2020, 4:57 PM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 1
Aaron Eberhardt Thank you very much. I did write the main part and the rest is from a book. The book talks about only some of the parts of program so I decided to compound them. Because I am learning hashtable and it is my first experince with it I did not know how to approach. The result is unreadeable code, I couldn't agree more. Thanks to you I'll rerwrite it considering the things you said. Have a nice day.
13th Apr 2020, 5:07 PM
mert aydın
+ 1
I'm sure your new code will look a lot better. Btw. a code formatter like clang-format can do most of the work for you. Have a nice day, too :)
13th Apr 2020, 7:03 PM
Aaron Eberhardt
Aaron Eberhardt - avatar