Articles

As you know, C programming language doesn’t have any built-in dictionary. There are plenty of implementation such as Apache APR and so on. But let’s build a simple one to know whats going on in the background.

We are going to implement a library which support creating dictionary and manipulating its items easily.

Basic implementation

Well, first we list our basic functions which are responsible to work with dictionary directly:

void dict_add_item(dict* d, char* key, char* value);
void dict_update_item(dict* d, char* key, char* value);
void dict_del_item(dict* d);
void dict_print(dict* d);

We store dictionary data in a struct :

typedef struct Dict {
    char *key;
    char *value;
} dict;

Required libraries are stdio.h , stdlib.h, string.h .

Let’s Implement dict_add_item function which is responsible to allocate memory and add passed data to created dictionary:

void dict_add_item(dict* d, char* key, char* value) {
    d->key = malloc(sizeof(char) * strlen(key));
    d->value = malloc(sizeof(char) * strlen(value));
    strcpy(d->key, key);
    strcpy(d->value, value);
}

Now, implement dict_update_item function which is responsible to update dictionary data and if it has different value size, it will reallocate the allocated memory:

void dict_update_item(dict* d, char* key, char* value) {
    if (strlen(d->value) != strlen(value)) {
        free(d->value);
        d->value = malloc(sizeof(char) * strlen(value));
    }
    strcpy(d->value, value);
}

Now, implement dict_del_item function which is responsible to destroy a dictionary and free allocated memory:

void dict_del_item(dict* d) {
    free(d->key);
    free(d->value);
}

Finally, implement dict_print function which is responsible to print dictionary like Python print function:

void dict_print(dict* d) {
    printf("{ %s: %s }\n", d->key, d->value);
}

Now let’s test our code by some sample data:

int main() {
    dict test;
    dict_add_item(&test, "name", "Sara");
    dict_print(&test);

    dict_add_item(&test, "name", "Mike");
    dict_print(&test);

    dict_del_item(&test);
    dict_print(&test);
}

Output:

{ name: Sara }
{ name: Mike }
{ : �fk��U }

Note: last line is nonsence because we released (free) the allocated area in memory.

So, it’s complete source code of part 1:

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

typedef struct Dict {
    char *key;
    char *value;
} dict;

void dict_add_item(dict* d, char* key, char* value) {
    d->key = malloc(sizeof(char) * strlen(key));
    d->value = malloc(sizeof(char) * strlen(value));
    strcpy(d->key, key);
    strcpy(d->value, value);
}

void dict_update_item(dict* d, char* key, char* value) {
    if (strlen(d->value) != strlen(value)) {
        free(d->value);
        d->value = malloc(sizeof(char) * strlen(value));
    }
    strcpy(d->value, value);
}

void dict_del_item(dict* d) {
    free(d->key);
    free(d->value);
}

void dict_print(dict* d) {
    printf("{ %s: %s }\n", d->key, d->value);
}

int main() {
    dict test;
    dict_add_item(&test, "name", "Sara");
    dict_print(&test);

    dict_add_item(&test, "name", "Mike");
    dict_print(&test);

    dict_del_item(&test);
    dict_print(&test);
}

This code works fine, but it doesn’t satisfy us. Because we are looking for a dictionary which provided multiple keys and values like this:

{"name": "Sara", "age": "27", "email": "[email protected]"}

But our code provides just one key/value as we’ve seen.

In the next section I will explain and implement how to provide this facility.

Tags:
All rights reserved ©