Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

store known key/value pairs in c

Tags:

c

key-value

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.

like image 483
Shylux Avatar asked Feb 06 '13 14:02

Shylux


People also ask

How do you store key-value pairs?

A key-value database is a type of nonrelational database that uses a simple key-value method to store data. A key-value database stores data as a collection of key-value pairs in which a key serves as a unique identifier. Both keys and values can be anything, ranging from simple objects to complex compound objects.

What is key-value pair in C?

C does not have a data structure of this type; in fact, the only collection type you have built in to C is the array. So, if you want something where you can supply a key and get the value, you have to roll your own or find one on the Internet.

What data type is used to store key-value pairs?

The dictionary stores objects as key-value pairs and can be used to represent complex real-world data.


1 Answers

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

like image 164
Urchin Avatar answered Oct 24 '22 05:10

Urchin