tags:

views:

56

answers:

4

Hello,

In Python it's easy:

x = {}
x['USD'] = "Dolars"
x['CLP'] = "Pesos"

or

y = {'lat': 23.678900, 'lng': 121.451928, 'name': "Sin City"}

I think most of these kind of problems are solved so where can I get information about dictionaries in C? I don't want to reinvent the wheel.

How do I implement a dictionary in C?

+1  A: 

They are called hash tables or hash maps.

There are lots of std ones for C++.
I wrote my own in C, or try this http://www.cl.cam.ac.uk/~cwc22/hashtable/

edit: Ok to split hairs - hash tables are one way to implement key:value pairs.

Martin Beckett
A: 

All your questions are answered here.

The idea: use a hash function avoiding collisions to use them as an index.

KikoV
A: 

glibc provides hcreate, hsearch, and hdestroy.

William Pursell
A: 

Hash tables are fine. If you want to stick to standard C library functions, there's also bsearch which is good for constant lookup dictionaries, or dynamic dictionaries in conjunction with qsort.

Doug Currie