diff --git a/20221212/tier-datenbank-15.c b/20221212/tier-datenbank-15.c new file mode 100644 index 0000000000000000000000000000000000000000..1fee6ab7722c1d05eb68a22d909a4bcf6175b4d6 --- /dev/null +++ b/20221212/tier-datenbank-15.c @@ -0,0 +1,68 @@ +#include <stdio.h> +#include <stdlib.h> + +#define ANIMAL 0 +#define WITH_WINGS 1 +#define WITH_LEGS 2 + +typedef struct animal +{ + int type; + char *name; +} animal; + +typedef struct with_wings +{ + int type; + char *name; + int wings; +} with_wings; + +typedef struct with_legs +{ + int type; + char *name; + int legs; +} with_legs; + +typedef union object +{ + animal a; + with_wings w; + with_legs l; +} object; + +object *new_wings (char *pname, int pwings) +{ + object *o = malloc (sizeof (with_wings)); + o->a.type = WITH_WINGS; + o->a.name = pname; + o->l.legs = pwings; + return o; +} + +object *new_legs (char *pname, int plegs) +{ + object *o = malloc (sizeof (with_legs)); + o->a.type = WITH_LEGS; + o->a.name = pname; + o->l.legs = plegs; + return o; +} + +int main (void) +{ + object *a[2] = { new_wings ("duck", 2), new_legs ("cow", 4) }; + + for (int i = 0; i < 2; i++) + { + if (a[i]->a.type == WITH_LEGS) + printf ("A %s has %d legs.\n", a[i]->a.name, a[i]->l.legs); + else if (a[i]->a.type == WITH_WINGS) + printf ("A %s has %d wings.\n", a[i]->a.name, a[i]->w.wings); + else + printf ("Error in animal: %s\n", a[i]->a.name); + } + + return 0; +}