store known key/value pairs in c

后端 未结 4 393
孤独总比滥情好
孤独总比滥情好 2020-12-30 05:21

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

4条回答
  •  [愿得一人]
    2020-12-30 05:37

    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.

    http://www.sparknotes.com/cs/searching/hashtables/section3.rhtml

提交回复
热议问题