diff --git a/20160121/loesung-1d-jawohl.c b/20160121/loesung-1d-jawohl.c new file mode 100644 index 0000000000000000000000000000000000000000..298c48557830359ca8a765fb6c18ac217f415313 --- /dev/null +++ b/20160121/loesung-1d-jawohl.c @@ -0,0 +1,54 @@ +#include <stdio.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; + +int main (void) +{ + animal *a[2]; + + with_wings duck; /* "animal duck" --> Absturz */ + duck.type = WITH_WINGS; + duck.name = "duck"; + duck.wings = 2; /* :-) */ + a[0] = (animal *) &duck; /* Jetzt erst ist das duck-Objekt fertig. + Zeiger-Zuweisung zuerst wäre aber auch zulässig. */ + with_legs cow; + cow.type = WITH_LEGS; + cow.name = "cow"; + cow.legs = 4; + a[1] = (animal *) &cow; /* Typumwandlung, weil "&cow" auf ein "with_legs" zeigt */ + + for (int i = 0; i < 2; i++) + if (a[i]->type == WITH_LEGS) + printf ("A %s has %d legs.\n", a[i]->name, + ((with_legs *) a[i])-> legs); + else if (a[i]->type == WITH_WINGS) + printf ("A %s has %d wings.\n", a[i]->name, + ((with_wings *) a[i])-> wings); + else + printf ("Error in animal: %s\n", a[i]->name); + + return 0; +} diff --git a/20160121/loesung-1d-nunja.c b/20160121/loesung-1d-nunja.c new file mode 100644 index 0000000000000000000000000000000000000000..e182489914a2825ddf5102e5a6eaf91e2d6d7b0c --- /dev/null +++ b/20160121/loesung-1d-nunja.c @@ -0,0 +1,55 @@ +#include <stdio.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; --> sonst Absturz */ + int wings; /* landet im Alignment-Dummy zwischen "int type" (32-Bit-Alignment) + und "char *name" (64-Bit-Alignment) */ +} with_wings; + +typedef struct with_legs +{ + int type; +/* char *name; --> sonst Absturz */ + int legs; +} with_legs; + +int main (void) +{ + animal *a[2]; + + animal duck; + a[0] = &duck; + a[0]->type = WITH_WINGS; + a[0]->name = "duck"; + ((with_wings *)a[0])->wings = 2; + + animal cow; + a[1] = &cow; + a[1]->type = WITH_LEGS; + a[1]->name = "cow"; + ((with_legs *) a[1])->legs = 4; + + for (int i = 0; i < 2; i++) + if (a[i]->type == WITH_LEGS) + printf ("A %s has %d legs.\n", a[i]->name, + ((with_legs *) a[i])-> legs); + else if (a[i]->type == WITH_WINGS) + printf ("A %s has %d wings.\n", a[i]->name, + ((with_wings *) a[i])-> wings); + else + printf ("Error in animal: %s\n", a[i]->name); + + return 0; +} diff --git a/20160121/loesung-1d.c b/20160121/loesung-1d.c new file mode 100644 index 0000000000000000000000000000000000000000..07bb014afb3dd7f043f0cf279327aad13ccbbd03 --- /dev/null +++ b/20160121/loesung-1d.c @@ -0,0 +1,54 @@ +#include <stdio.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; + +int main (void) +{ + animal *a[2]; + + with_wings duck; /* "animal duck" --> Absturz */ + a[0] = &duck; + a[0]->type = WITH_WINGS; + a[0]->name = "duck"; + ((with_wings *)a[0])->wings = 2; /* Die Elemente von a[] sind Zeiger + auf "animal"s. */ + with_legs cow; + a[1] = &cow; + a[1]->type = WITH_LEGS; + a[1]->name = "cow"; + ((with_legs *) a[1])->legs = 4; + + for (int i = 0; i < 2; i++) + if (a[i]->type == WITH_LEGS) + printf ("A %s has %d legs.\n", a[i]->name, + ((with_legs *) a[i])-> legs); + else if (a[i]->type == WITH_WINGS) + printf ("A %s has %d wings.\n", a[i]->name, + ((with_wings *) a[i])-> wings); + else + printf ("Error in animal: %s\n", a[i]->name); + + return 0; +}