Re: Associative Array in C



On Mar 2, 8:24 pm, chadsspameaterem...@xxxxxxxxx wrote:
What I am trying to do is implement a associative array to lookup data
based on a key in C.  This might simple to do in C++ or Perl but I'm
restricted to use C.  The size of the data for the table isn't too
large but too large to structure the code as a clean chain of if/else
or similar logic.  Also, the lookup key values can be large,
unpredictable and not well dispersed which doesn't suit itself to a
hash table.

OK so my current idea for implementing this is using an array of C
structures which gets initialized and contains all the relevant
information.  The array will be a module global variable used in the C
file which needs this functionality.  The array definition would look
something like the following:

struct ErrorLookupTable
   {
   long  DeviceErrorCode;
   char* ErrorString;
   };

#define TableSize 6

static struct ErrorLookupTable MyTable[ TableSize ] =
   {
      {     45, "Not enough memory" },
      {    -66, "File I/O error" },
      {  -2565, "Device locked" },
      {  32727, "Device not found" },
      { -32727, "Device not found" },
      {  65534, "Unknown device error" }
      //... LOTS more
   };

So I have implemented some prototype code based on this and it does
work to do lookups.  But what I was wondering is how portable is
this?  Is this legal C as defined by the standard to initialize an
array of structures in this way?  Is this going to work across
platforms? I hope you see my dilemma the code appears to work on my
system. But I don't know if it's guaranteed to work everywhere and how
portable the library functions I'm working on will be because of it?

If you provided the errors in sorted order, you could use bsearch to
do the lookup.
I guess that a hash table will work perfectly well and be the best
solution for your problem.
If you don't want to use a hash table, then provided the data in
sorted order or use a skiplist.

P.S.
Besides the good suggestions to use sizeof, I would also suggest
making everything const.
.



Relevant Pages

  • Re: Collections of structured-data objects: what approach?
    ... you don't need 4 (lookup by key) an Array is perfect. ... then it's desirable to have an index for the array on the ... But if you are going to implement an index using a helper hash, ... Microsoft Visio MVP ...
    (comp.lang.ruby)
  • Re: Collections of structured-data objects: what approach?
    ... by doing this you can chain methods. ... you don't need 4 (lookup by key) an Array is perfect. ... For lookup, then it's desirable to have an index for the array on the key field, which can be a ruby hash. ...
    (comp.lang.ruby)
  • Re: hash tables, non-random keys
    ... large numbers of insertions, or I would just use an array. ... optimize hash tables). ... I guess then the issue is how frequent lookup fails are... ...
    (comp.programming)
  • Re: hash tables, non-random keys
    ... large numbers of insertions, or I would just use an array. ... optimize hash tables). ... I guess then the issue is how frequent lookup fails are... ...
    (comp.programming)
  • Re: Idea for ECMA/C# Standard - compile time hash for performance
    ... contains 100 fields that I do hash lookups on to get and set the values. ... counting the lookup then and processing. ... this could be done with a straight array lookup using the minimal memory ... > The key is I don't want to burn a huge amount of memory and I want to incur ...
    (microsoft.public.dotnet.languages.csharp)