/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>

enum one_or_other { ONE, OTHER };

struct many_things
{
    char *name;
    enum one_or_other what;
    void *something;
};

struct one_thing
{
    int iv;
};

struct other_thing
{
    double xv;
};

void dump(struct many_things *o)
{
    struct one_thing *ont;
    struct other_thing *ott;
    printf("name=%s\n", o->name);
    switch(o->what)
    {
        case ONE:
            ont = o->something;
            printf("iv=%d\n", ont->iv);
            break;
        case OTHER:
            ott = o->something;
            printf("xv=%f\n", ott->xv);
            break;
        default:
            /* we should do something bad here */
            ;
    }
}

int main()
{
    struct many_things a, b;
    struct one_thing a2;
    struct other_thing b2;
    a2.iv = 123;
    a.name = "A";
    a.what = ONE;
    a.something = &a2;
    dump(&a);
    b2.xv = 123.456;
    b.name = "B";
    b.what = OTHER;
    b.something = &b2;
    dump(&b);
    return 0;
}

