ckey-value

store known key/value pairs in c


I'm currently learning c. I'm writing a web server as an exercise.
Now i have to store the status codes and reason phrases.

What is the best way to store those key/value pairs?

My first bet was a hashmap. But there is no native implementation in c. So i would have to use a library.


Solution

  • Like other answers, I would also recommend just using an array of strings as a lookup table. If you assume all the status codes are unique, an array of strings is by far the easiest implementation for a smaller set of data.

    Once you start storing larger amounts of data, that is when hashmaps start becoming useful. A lookup array is the solution here, but as you said you're learning C, you can actually implement a hashtable in native C by using dynamic memory (a critical concept to learn for C.) This website explains how to create a hashtable in C very well.

    https://web.archive.org/web/20240525073316/https://www.sparknotes.com/cs/searching/hashtables/section3/