3

In php I can crate a associative array like

$my_arr = array('a' => 'Apple', 'b' => 'Ball', 'c' => 'Cat');

Is it possible to create a associative array in C

You may think it is duplicate question for Associative arrays in C But I can't found what I want.

MAChitgarha
  • 3,728
  • 2
  • 33
  • 40
Md. Sahadat Hossain
  • 3,210
  • 4
  • 32
  • 55

3 Answers3

3

There's no prebuilt utility in standard C to accomplish this task. Your best shot is to try to implement a red-black tree (or an hash table for non ordered associative containers) on your own.

If you have the luck to work with C++, on the other hand, you can use std::map. Here's an example for std::map:

std::map<char, std::string> map;
map['a'] = "Apple";
map['b'] = "Ball";
map['c'] = "Cat";

Live example

Shoe
  • 74,840
  • 36
  • 166
  • 272
  • 4
    The question is asking for an associative array in C not C++, not a valid answer – Khaled.K Apr 07 '14 at 09:41
  • @KhaledAKhunaifer, How is "There's no prebuilt utility in standard C to accomplish this task." not a valid answer exactly? – Shoe Apr 07 '14 at 10:15
  • @KhaledAKhunaifer, also, the question was [initially tagged with C++ as well](http://stackoverflow.com/revisions/22908516/1). – Shoe Apr 07 '14 at 10:17
  • 1- I see a `C` tag. 2- `C++` tag was removed. 3- The question asks `Is it possible to create a associative array in C`. 4- the question state that his question is not a duplicate of another question asking for an associative array in C. – Khaled.K Apr 07 '14 at 10:40
  • "How is "There's no prebuilt utility in standard C to accomplish this task." not a valid answer exactly?" That should've been a comment, not an answer – Khaled.K Apr 07 '14 at 10:44
  • 1
    @KhaledAKhunaifer Unfortunately for you, "there's no standard utility in C to accomplish this task. Your best bet is to try implementing a red-black tree on your own" is a perfectly valid answer to the question "Is it possible to create a associative array in C?". – Shoe Apr 07 '14 at 11:03
  • 1
    You can also implement a hash table instead of a red-black tree if you don't care about the order of the keys. –  Apr 07 '14 at 11:08
3

Really robust and simple way to do it is to have a struct with key and value fields. Lets call it pair (name derived from C++ class of the same name and purpose). Also you should think of the types you want have for the pair fields. I give an example as char to string values as your php example. But in order to use different types you must use void*, but that will result in a very complicated and probably bug prone implementation.

Something like

struct
{
    char key;
    char* value;
}pair;

struct pair map[size];

pair assocation;
assocation.key = 'a';
assocation.value = "Apple"; // Be sure to allocate the C strings so that you do not introduce memory leak or data corruption, same for void*. This here is just an hack.

map[0] = assocation;  

// Later in your algorithms and parsers you just access it as an value in array.  
pair aPair = map[1];
char aKey = aPair.key;
char* aValue = aPair.value;

When you want a linked list like associative array, then add one more field to a struct:

void* nextPair;  

With that you can allocate you key-value pairs everywhere and do not need to contain them in a single array.

arapEST
  • 491
  • 1
  • 4
  • 16
0

As others have said, C is too low-level to have a built-in associative array type. Probably the closest you can get to it is to use a third party library such as http://svn.freebsd.org/base/head/sys/sys/tree.h, documented in http://www.freebsd.org/cgi/man.cgi?tree or http://ivoras.sharanet.org/freebsd/usetree.html.

Ivan Voras
  • 1,895
  • 1
  • 13
  • 20