0

In C, can you create a dictionary? I come from a Objective-C background so I would like to know if there is anything similar to NSDictionary.

1

4 Answers 4

7

You can create anything you want in C. You just won't have native language support for most of it.

2

You can create a dictionary in C, but there is no dictionary built in to the standard C library.

A quick search on Google code shows that there are open-source (and generously licensed) C dictionary implementations here and here.

1

Posix does have a limited hash table -- see hcreate(), hsearch() and hdestroy() that can be used by a C program.

A discussion of the limitations appears in this stackoverflow question.

1
  • 1
    The GNU library adds hcreate_r(), hsearch_r(), and hdestroy_r(), which allow more than one hash table in a program. Feb 23, 2012 at 4:49
0

Without OOP and templates, it would be hard to implement a hash table or a balanced tree that is truly general, easy to use and performant, and therefore worthy to be in the run-time library that comes with the language.

That being said, you can always implement your own, or just use C++ (see unordered_map or map).

7
  • Performant is not really a word, although it will become one because people keep using it. I assume you mean it in the sense that the data structure/functions must be efficient. Why do you think C implementations of hash tables and balanced trees would be any less efficient than OOP C++ implementations? Feb 23, 2012 at 4:58
  • (Didn't get to finish my comment - got the dreaded "only edit every 5 seconds" message!) I wrote a general-purpose hash table package in C that was easy-to-use and fast - a table of 80,000 telemetry points had a mean number of comparisons slightly over 1, points within 1 standard deviation were still under 2 comparisons, and points within 2 standard deviations were slightly over 2 comparisons. Feb 23, 2012 at 5:15
  • 1
    @ranko: Huh? You build a tree/hash that stores void pointers and accepts a comparison function with a prototype of int cmp(void *, void *); and returns -1/0/1. This has been done for years, is easy to use, and creates a general-purpose tree/hash in C (e.g., see the standard C library bsearch API).
    – tbert
    Feb 23, 2012 at 5:42
  • @AlexMeasday I said "general, easy to use and performant", not just performant. One can certainly hand-code a performant implementation specific to a particular element type and hash and equality functions, but to do it so it works for any type and hash/equality functions, you'd need data and function pointers, compromising the ease of use and probably performance. You could possibly use macros, but this no longer qualifies as "C" and has its own problems. I can't judge your implementation, but it would be interesting to benchmark it against C++ unordered_set... Feb 23, 2012 at 6:00
  • 1
    @BrankoDimitrijevic C is not a type-safe language. And I've chased (and solved!) these bugs in software myself. Helper functions exist for just this purpose (i.e., make sure that your calls to the tree/whatevah are correct by abstracting it into a get/set interface).
    – tbert
    Feb 23, 2012 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.