Skip to content
Snippets Groups Projects
Select Git revision
  • 7ed81b884715d26ff30e30f8b169bf1cb50868cd
  • master default
2 results

objects-4.c

Blame
  • user avatar
    Peter Gerwinski authored
    7ed81b88
    History
    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;
    }