By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,669 Members | 1,654 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,669 IT Pros & Developers. It's quick & easy.

Associative Array in C?

P: 2
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++ 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 initalize 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?

After defining the structure as above I would do the lookups with code similar to the following:

while ( ( Counter < TableSize ) && ( MatchFound == 0 ) )
if ( ErrorCode == MyTable[ Counter ].DeviceErrorCode )
MatchFound = 1;
StringToReturn = MyTable[ Counter ].ErrorString;

Counter = Counter + 1;

I do have a secondary question as well. If the above works and is portable then I wonder if rather then using "#define TableSize 6" to define the table size if I just wanted it to be calculated dynamically could I do this as well? I'd calculate the table size with something like the following:

TableSize = sizeof( MyTable ) / sizeof( struct ErrorLookupTable );

Would this work or code similar to this? I am writing C library functions which need to be portable. That's why I'm wondering if all this is standard C as defined by the standard or not. Thanks. :)
Mar 3 '08 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 1,275
The code you have written is Standard C(ANSI).

Mar 3 '08 #2

Expert Mod 5K+
P: 9,197
TableSize = sizeof( MyTable ) / sizeof( struct ErrorLookupTable );
Be careful of this. sizeopf reports the size of the object on the stack.

When MyTable is passed to a function, only the address passes so your sizeof will report the size of the address and not the array.

Personally, I would use a linked list for this.

Avoid static or global variables. See the article inthe C/C++ HowTos on The Case Against Global Variables.
Mar 3 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.