Dumping a Phony Associative Array

The first phony associative array function I need to write is a simple dump: Output the array’s contents in the form of pairs. Performing this operation requires a bit of manipulation to the way the array is presented in the code.

Starting with the example from last week’s Lesson, I modified the array so that it’s a single dimension array of strings, yet presented as a series of pairs:

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

    puts("Array Dump");
    array_dump(months);

    return(0);
}

The new phony-associative array looks cleaner, plus it’s easier to work with as a single dimension char pointer array. Specifically, look at the array_dump() function, which requires only the array’s name as an argument; no double-pointer nonsense. Though keep in mind that this function assumes the array’s contents are strings.

Also note that the final pair in the array are NULLs. This termination allows me to be flexible with my array_dump() function, eliminating a size argument.

To output or “dump” the array, the array_dump() function churns through each pair in the array, outputting the keys and values each on a line:

void array_dump(char *a[])
{
    int x = 0;

    while( a[x] )
    {
        printf("'%s' => '%s'\n",
                a[x],
                a[x+1]
              );
        x+=2;
    }
}

The function’s argument uses the same format as the months[] array declared in the main() function. Again, no double-asterisk nonsense.

The while loop spins as long as the value of a[x] isn’t NULL. Reference variables a[x] and a[x+1] represent each pair in the phony-associative array. The x+=2 statement notches up the offset to the next pointer pair in the array. Here’s sample output:

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

Click here to view the full code on GitHub.

Another dumping function commonly found in programming languages that deal with associative arrays just outputs the keys. Here is such a function, key_dump():

/* assumes `char` array */
void key_dump(char *k[])
{
    int x = 0;

    while( k[x] )
    {
        printf("'%s'\n",
                k[x]
              );
        x+=2;
    }
}

The key_dump() function is a slight modification of array_dump(), just eliminating the value part of the array’s output. Here’s a sample run:

Key dump:
'January'
'February'
'March'
'April'
'May'
'June'
'July'
'August'
'September'
'October'
'November'
'December'

You can view the full code on GitHub here.

For next week’s Lesson, I craft a key() function, which returns the value part of the phony-associative array when the key is supplied.

3 thoughts on “Dumping a Phony Associative Array

  1. JUILLET ! ! ! ! !

    Using nulls is a nifty idea but if you’re going to write functions to add and delete key/value pairs it might be fiddly shuffling them around. I’d probably create a dictionary struct with the array itself (or two arrays for keys and values) and also the current size.

    I don’t agree that yours is a phony associative array just because it’s not built in to C. Creating data structures that aren’t provided by the language you are using is a perfectly legitimate thing to do.

  2. The data structures approach is what I’d use if I were to implement adding and removing pairs from the list. Then the NULL issue resolves itself because it would be a linked list. That’s the true solution.

    This series of Lessons evolved from a suggestion I think you made a few weeks ago regarding pairs of arrays. I forget the specifics, but this is the direction I took it.

  3. That was replacing characters in C code with HTML compatible symbols or strings, the ASCII codes indexing the array. It’s not ideal but it’s a reasonable quick botch for times when that is acceptable. I used it for a simple utility purely for my own use. (Although I did inflict it on the outside world on Github).

    Dictionaries can also be implemented as a hash table, where some sort of algorithm is applied to the key to get the index. I saw an example once where the ASCII codes for each character in the key were added, and then the total square rooted. Something like that anyway.

Leave a Reply