php - Associative array in C. -


in php can crate associative array like

$my_arr = array('a' => 'apple', 'b' => 'ball', 'c' => 'cat'); 

is possible create associative array in c

you may think duplicate question associative arrays in c can't found want.

really robust , simple way have struct key , value fields. lets call pair (name derived c++ class of same name , purpose). should think of types want have pair fields. give example char string values php example. in order use different types must use void*, result in complicated , bug prone implementation.

something

struct {     char key;     char* value; }pair;  struct pair map[size];  pair assocation; assocation.key = 'a'; assocation.value = "apple"; // sure allocate c strings not introduce memory leak or data corruption, same void*. here hack.  map[0] = assocation;    // later in algorithms , parsers access value in array.   pair apair = map[1]; char akey = apair.key; char* avalue = apair.value; 

when want linked list associative array, add 1 more field struct:

void* nextpair;   

with can allocate key-value pairs everywhere , not need contain them in single array.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -