473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Associative Array in C?

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
2 3391
gpraghuram
1,275 Expert 1GB
The code you have written is Standard C(ANSI).


Raghuram
Mar 3 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
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

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

Similar topics

11
by: Stefan Richter | last post by:
Hi, I want to create an associative Array with a PHP variable (article ID) as Key and another associative array as it's value. How do I instanciate it, how can I fill it? I want something...
27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
6
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
4
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
8
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
5
by: soup_or_power | last post by:
Hi I have an associative array like this: arr=30; arr=20;arr=40;arr=10; I want the sort function to sort keys in ascending order of the values on the right hand side with the following result:...
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.