Question

I have a problem right now where I'm trying to essentially bind a string to a value. What would be the easiest way to associate these values in C? I essentially want a Python like tuple that I can add to a list.

Was it helpful?

Solution

That's what C structures are for. For example, the following structure is a tuple which binds a string (up to twenty characters long) to an integer value:

typedef struct {
    char strVal[21];
    int intVal;
} tTuple;

Variables of type tTuple (or pointers to them if they're dynamically allocated) can be passed around, added to lists, and manipulated in any way that makes sense to your situation.


Using a structure similar to the one above, the following complete program shows this in action. It could probably do with some more sanity checks(a) but should be fine for just showing how to do a tuple (which is what the question was asking about):

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

static struct { char strVal[21]; int intVal; } tuple[10];
static int tupleCount = 0;

static void listTuples(void) {
    printf("==========\nTuple count is %d\n", tupleCount);
    for (int i = 0; i < tupleCount; ++i)
        printf("   [%s] -> %d\n", tuple[i].strVal, tuple[i].intVal);
    puts("==========");
}

static void addTuple(char *str, int val) {
    printf("Adding '%s', mapped to %d\n", str, val);
    strcpy(tuple[tupleCount].strVal, str);
    tuple[tupleCount++].intVal = val;
}

static void deleteTuple(char *str) {
    int index = 0;
    while (index < tupleCount) {
        if (strcmp(str, tuple[index].strVal) == 0) break;
        ++index;
    }
    if (index == tupleCount) return;

    printf("Deleting '%s', mapped to %d\n", str, tuple[index].intVal);
    if (index != tupleCount - 1) {
        strcpy(tuple[index].strVal, tuple[tupleCount - 1].strVal);
        tuple[index].intVal = tuple[tupleCount - 1].intVal;
    }
    --tupleCount;
}

int main(void) {
    listTuples();

    addTuple("aardvark", 31);
    addTuple("buffalo", 41);
    addTuple("camel", 59);
    addTuple("dingo", 27);
    listTuples();

    deleteTuple("buffalo");
    listTuples();

    return 0;
}

The output of that program is:

==========
Tuple count is 0
==========
Adding 'aardvark', mapped to 31
Adding 'buffalo', mapped to 41
Adding 'camel', mapped to 59
Adding 'dingo', mapped to 27
==========
Tuple count is 4
   [aardvark] -> 31
   [buffalo] -> 41
   [camel] -> 59
   [dingo] -> 27
==========
Deleting 'buffalo', mapped to 41
==========
Tuple count is 3
   [aardvark] -> 31
   [dingo] -> 27
   [camel] -> 59
==========

(a) Such as checking the string length and array count for overflow, or disallowing duplicate keys (if desired).

OTHER TIPS

An alternate way to associate values of different types is to create parallel arrays. One array for each typed element, associated by having the same index.

char *strVal[5];
int intVal[5];

So strVal[0] is associated with intVal[0], and so on.

This way of representing a tuple can be applied even in a language without a struct or record type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top