Reputation: 659
I am trying to write something in c that relates two arrays to each other but I am very novice in c language and I don't want to start off with the wrong path... What I need is something like python's dict and I am thinking of using a struct. Is this the way to go or is there something I'm missing?
Thanks ;)
Upvotes: 1
Views: 209
Reputation: 733
Search for the tag hastable
or text implementation in C
here in SO and you'll find some examples and also interesting problems and solutions worth reading.
Upvotes: 1
Reputation: 500277
A Python dict
and a C struct
are very different beasts.
A struct
can be thought of as a way to group related variables together, so that they can be passed around as a single unit.
A dict
is a mapping from keys to values, where the set of keys is generally not known at compile time. If you need to do something similar in C, your best bet is probably to use a library of standard data structures. These have been discussed in the past: Are there any open source C libraries with common data structures?
There are several standard data structures that can enable one to map keys to values. A hash table is a sensible default choice.
Upvotes: 2
Reputation: 33252
I think you are looking for an HashTable. I just googled for an example, but look around I'm sure you will find an implementation example satisfying your needings.
Upvotes: 2