Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Associative arrays in C

I am implementing a way to transfer a set of data to a programmable dongle. The dongle is based on a smart card technology and can execute an arbitrary code inside. The input and output data is passed as a binary blocks that can be accessed via input and output pointers.

I would like to use an associative array to simplify the data processing code. Everything should work this way:

First the host application:

// Host application in C++ in_data["method"] = "calc_r"; in_data["id"] = 12; in_data["loc_a"] = 56.19; in_data["loc_l"] = 44.02; processor->send(in_data); 

Next the code inside the dongle:

// Some dongle function in C char* method_name = assoc_get_string(in_data, "method"); int id = assoc_get_int(in_data, "id"); float loc_a = assoc_get_float(in_data, "loc_a"); float loc_l = assoc_get_float(in_data, "loc_l"); 

So my question is about the dongle part functionality. Is there C code or library to implement such an associative array behavior like the above?

like image 928
ezpresso Avatar asked Feb 01 '11 15:02

ezpresso


People also ask

What is associative array in C?

Associative arrays in C++ C++Server Side ProgrammingProgramming. In c++ programming language, an associative array is a special type of array in which the index value can be of any data type i.e. it can be char, float, string, etc. These associative arrays are also known as maps or dictionaries.

What is associative array with example?

Associative arrays are used to store key value pairs. For example, to store the marks of different subject of a student in an array, a numerically indexed array would not be the best choice.

What does associative array do?

Associative arrays, also called maps or dictionaries, are an abstract data type that can hold data in (key, value) pairs. Associative arrays have two important properties. Every key can only appear once, just like every phone number can only appear once in a directory.


1 Answers

Glib's hash table. implements a map interface or (associative array). And it's most likely the most used hash table implementation for C.

GHashTable *table=g_hash_table_new(g_str_hash, g_str_equal);  /* put */ g_hash_table_insert(table,"SOME_KEY","SOME_VALUE");  /* get */ gchar *value = (gchar *) g_hash_table_lookup(table,"SOME_KEY"); 
like image 164
Manuel Salvadores Avatar answered Oct 10 '22 16:10

Manuel Salvadores