How would I go about making this table?

ID    Type   Value
X     int    4
Y     int    4
Z     float  0

This is the only way I can think of but I don't think this will work.

int main(int argc, char *argv[])
{
    char *strings_line1[3] = {"ID", "TYPE", "Value"};
    char *strings_line2[3] = {"X", "int", "4"};
    char *strings_line3[3] = {"Y", "int", "4"};
    char *strings_line4[3] = {"Z", "float", "0"};
    return 0;
}

Is this considered a symbol table? Whats the difference between a table and symbol table?

Edited 8 Months Ago by COKEDUDE

This would be simple in C++ as you can create a number class that you can derive new types from and store a pointer to the base class in a collection. In any case, you should have a two two dimensional array defined like this:

const char* array[][3] = { {"ID", TYPE", "Value"}, {"X", "int", "4"}, ... };

Here is a sample program:

#include <stdio.h>

int main()
{
    const char* array[][3] = {
        {"ID", "TYPE", "Value"},
        {"X", "int", "4"},
        {"Y", "int", "4"},
        {"Z", "float", "0.0"},
        {NULL, NULL, NULL}};
    size_t i = 0;
    for (i = 0; array[i][0] != NULL; i++)
    {
        printf("%s\t%s\t%s\n", array[i][0], array[i][1], array[i][2]);
    }
    return 0;
}

Next you need to add code to turn these strings into real values. In any case, the output of this program is:

ID  TYPE    Value
X   int     4
Y   int     4
Z   float   0.0

Edited 8 Months Ago by rubberman

As for the difference between a table like this and a symbol table is that a symbol table would contain symbols. You can use either an enum or #define statements to define your symbols, which can also b used in your array, and typically that data would be stored in a struct which would go into the array. Example:

typedef enum {
    anInt = 0,
    aFloat,
    aDouble,
    aString,
    end_of_symbol_types
} symbol_types;

typedef struct {
    const char* sname;
    symbol_types stype;
    union {
        int intVal;
        float floatVal;
        double doubleVal;
        const char* stringVal;
    };
} symbol_t;

symbol_t sarray[] = {
    { "X", anInt, (int)4 },
    { "Y", anInt, (int)4 },
    { "Z", aFloat, (float)0.0 },
    { NULL, end_of_symbol_types, (int)-1 }};

Edited 8 Months Ago by rubberman

From the stype, you can determine how to output the value, and which element of the union where you will find it. Another example:

for (i = 0; sarray[i].sname != NULL; i++)
{
    switch (sarray[i].stype)
    {
        case anInt:
            printf("%s\t%s\t%d\n",
                sarray[i].sname,
                "int",
                sarray[i].intVal);
            break;
        case aFloat:
            printf("%s\t%s\t%F\n",
                sarray[i].sname,
                "float",
                sarray[i].floatVal);
            break;
        case aDouble:
            printf("%s\t%s\t%F\n",
                sarray[i].sname,
                "double",
                sarray[i].doubleVal);
            break;
        case aString:
            printf("%s\t%s\t%d\n",
                sarray[i].sname,
                "char",
                sarray[i].stringVal);
            break;
        default: break; /* invalid type */
    }
}

There are some problems with the symbol examples above. I will repost later with corrections. The values were not being properly initialized. Sorry about that... :-)

This is better:

#include <stdio.h>

typedef enum {
    anInt = 0,
    aFloat,
    aDouble,
    aString,
    end_of_symbol_types
} symbol_types;

typedef union {
        int intVal;
        double doubleVal;
        float floatVal;
        const char* stringVal;
} sym_val;

typedef struct {
    const char* sname;
    symbol_types stype;
    sym_val sval;
} symbol_t;


int main()
{    
    symbol_t sarray[5];
    sarray[0].sname = "X";
    sarray[0].stype = anInt;
    sarray[0].sval.intVal = 4;

    sarray[1].sname = "Y";
    sarray[1].stype = anInt;
    sarray[1].sval.intVal = 4;

    sarray[2].sname = "Z";
    sarray[2].stype = aFloat;
    sarray[2].sval.floatVal = 10.0F;

    sarray[3].sname = "S";
    sarray[3].stype = aString;
    sarray[3].sval.stringVal = "xyzzy";

    sarray[4].sname = NULL;

    size_t i;
    printf("ID\tTYPE\tVALUE\n");
    for (i = 0; sarray[i].sname != NULL; i++)
    {
        switch (sarray[i].stype)
        {
            case anInt:
                printf("%s\t%s\t%d\n",
                    sarray[i].sname,
                    "int",
                    sarray[i].sval.intVal);
                break;
            case aFloat:
                fprintf(stdout, "%s\t%s\t%f\n",
                    sarray[i].sname,
                    "float",
                    (double)sarray[i].sval.floatVal);
                break;
            case aDouble:
                printf("%s\t%s\t%F\n",
                    sarray[i].sname,
                    "double",
                    (double)sarray[i].sval.doubleVal);
                break;
            case aString:
                printf("%s\t%s\t%s\n",
                    sarray[i].sname,
                    "char",
                    (const char*)sarray[i].sval.stringVal);
                break;
            default: break; /* invalid type */
        }
    }
    return 0;
}

Edited 8 Months Ago by rubberman

This article has been dead for over six months. Start a new discussion instead.