Associative Arrays

Being the ancient and venerable programming language it is, C lacks the associative array data type. This type of array is found in other languages, and it can be awkwardly simulated in C.

From last week’s Lesson, I sought to create two arrays, each with corresponding words in French and English. This dual array approach seemed to be the best way to translate nouns between the language, though I found my solution to be clumsy. What I really need is an associative array, which is known as a dictionary in Python.

An associative array contains pairs of keys and values. You can use the keys to summon a value. For example:

{ { "One", "First" },
{ "Two", "Second" },
{ "Three", "Fourth" } }

In this pseudocode example, the associative array contains three elements or pairs. For the key One the value is First. The second element has the key Two and the value Second. The last element key is Three with the value Third.

The key/value pairs need not be strings. As with an array, they can any data type. For example:

{ { "Zero", 0.0 },
{ "e", 2.71828 },
{ "pi", 3.14159 } }

The key/values need not be consistent, either. Above, the keys are strings with real numbers values.

Associative arrays are used like any array, though the keys summon the values. For example:

printf("You're score ranked %s\n",rank["One"]);

The above statement fetches element "One" from the associative array rank[]. From the example shown earlier, the value output is the string "First".

In a way, the C language does use associative arrays, though the keys are locked in numeric order. For example:

{ { 1, "January" },
{ 2, "February" },
{ 3, "March" },
etc.

To create a simulated associative array requires a bit of overhead, more than necessary in other programming languages. The following code shows my stab at an associative array to do my English/French translation.

2023_06_10-Lesson.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    const char *months[12][2] =
    {
        { "January", "janvier" },
        { "February", "fevrier" },
        { "March", "mars" },
        { "April", "avril" },
        { "May", "mai" },
        { "June", "juin" },
        { "July", "jullet" },
        { "August", "aout" },
        { "September", "septembre" },
        { "October", "octobre" },
        { "November", "novembre" },
        { "December", "decembre" }
    };
    int x;

    for( x=0; x<12; x++ )
        printf("%s = %s\n",
                months[x][0],
                months[x][1]
              );

    return(0);
}

The months[] array contains pairs of char pointers. It’s a two dimensional array, with each element representing a pair of strings. The program outputs each pair, though it doesn’t use key/value references.

January = janvier
February = fevrier
March = mars
April = avril
May = mai
June = juin
July = jullet
August = aout
September = septembre
October = octobre
November = novembre
December = decembre

In those programming languages that support associative arrays, functions (or methods) are available that fetch the keys and values separately. Functions to dump an associative array are also available. Even then, some languages may even let you perform tricks like this:

for( x="January"; x<="December"; x++ )

In C, however, more overhead and effort is required to deal with data pairs such as those found in this Lesson's months[] array. In next week's Lesson I explore various ways to further foist an associative array into the C language.

Leave a Reply