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

objects-4.c

Blame
  • Forked from Peter Gerwinski / hp
    341 commits behind the upstream repository.
    objects-4.c 562 B
    #include <stdio.h>
    
    typedef struct
    {
      int type;
    } t_base;
    
    typedef struct
    {
      int type;
      int content;
    } t_integer;
    
    typedef struct
    {
      int type;
      char *content;
    } t_string;
    
    int main (void)
    {
      t_integer i = { 1, 42 };
      t_string s = { 2, "Hello, world!" };
    
      t_base *object[] = { (t_base *) &i, (t_base *) &s };
    
      for (int i = 0; i < 2; i++)
        if (object[i]->type == 1)
          printf ("Integer: %d\n", (t_integer *) object[i]->content);
        else if (object[i]->type == 2)
          printf ("String: \"%s\"\n", (t_string *) object[i]->content);
    
      return 0;
    }