Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
Associative Array in C
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Guest, post: 3464390"] 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? Thanks. :) [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
Associative Array in C
Top