Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C

Name: Anonymous 2011-07-20 4:17

I'm sick of seeing the children here posting about their lisps and their pythons and what not.

Let's have a thread for a manly programming language for men like C.

Name: Anonymous 2011-07-22 6:05

How is my map implementation?

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

#define MAX_STR_LEN 64
#define MAX_KEYS 1000

struct map {
        int count;
        char key[MAX_KEYS][MAX_STR_LEN];
        char value[MAX_KEYS][MAX_STR_LEN];
};

char *map_fetch(struct map *map, char *key);
void map_store(struct map *map, char *key, char *value);

int main(void) {
        struct map my_map;
        my_map.count = 0;
       
        map_store(&my_map, "sussman", "God");
        map_store(&my_map, "mdickie", "artist");
        map_store(&my_map, "frozen-void", "autist");
        map_store(&my_map, "VIPPER", "anus");

        printf("sussman-> %s\n", map_fetch(&my_map, "sussman"));
       
        return 0;
}


char *map_fetch(struct map *map, char *key) {
        int i;
        for (i=0; i <= map->count; i++) {
                if (!strcmp(map->key[i], key)) {
                        return map->value[i];
                }
        }
        return NULL;
}


void map_store(struct map *map, char *key, char *value) {
        int i = map->count;
        if (i == MAX_KEYS) { return; } // silent failure is kinda lame
        strcpy(map->key[i], key);
        strcpy(map->value[i], value);
        map->count++;
}

Newer Posts