Skip to content
Snippets Groups Projects
Select Git revision
  • d48edcd327479e7a230e1a34684f251a5cd47337
  • master default protected
  • 2018ws
  • 2017ws
  • 2016ws
5 results

side-effects-14.c

Blame
  • Forked from Peter Gerwinski / hp
    Source project has a limited visibility.
    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;
    }