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
3 ответов
+ 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 :)
+ 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.
+ 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 :)