Skip to content
Snippets Groups Projects
Select Git revision
  • 4ff440eaac9aee21bd0a6c7f5ce7927ee4cd9b94
  • master default
2 results

aufgabe-2b.c

Blame
  • Forked from Peter Gerwinski / hp
    341 commits behind the upstream repository.
    aufgabe-2b.c 886 B
    #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 wings;
    } with_wings;
    
    typedef struct with_legs
    {
      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;
    }