Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Associative Arr...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 7 Topic 24769 of 28006
Post > Topic >>

Associative Array in C

by chadsspameateremail@[EMAIL PROTECTED] Mar 2, 2008 at 08:24 PM

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 ****table 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
****table the library functions I'm working on will be because of it?

Thanks. :)
 




 7 Posts in Topic:
Associative Array in C
chadsspameateremail@[EMAI  2008-03-02 20:24:01 
Re: Associative Array in C
Friedrich Dominicus <j  2008-03-03 07:36:50 
Re: Associative Array in C
Ulrich Eckhardt <dooms  2008-03-03 07:56:49 
Re: Associative Array in C
user923005 <dcorbit@[E  2008-03-03 12:55:47 
Re: Associative Array in C
chadsspameateremail@[EMAI  2008-03-03 14:33:44 
Re: Associative Array in C
chadsspameateremail@[EMAI  2008-03-03 14:35:53 
Re: Associative Array in C
CBFalconer <cbfalconer  2008-03-03 17:43:04 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Nov 21 12:22:51 CST 2008.