Skip to content
Snippets Groups Projects
Commit f5168616 authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Weitere Beispiele zur "Tier-Datenbank"-Aufgabe

parent cb36e7fe
Branches
No related tags found
No related merge requests found
#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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment