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

objects-3.c

Blame
  • objects-7.c 707 B
    #include <stdio.h>
    
    #define T_BASE    0
    #define T_INTEGER 1
    #define T_STRING  2
    
    typedef struct
    {
      int type;
    } t_base;
    
    typedef struct
    {
      int type;
      int content;
    } t_integer;
    
    typedef struct
    {
      int type;
      char *content;
    } t_string;
    
    void print_object (t_base *this)
    {
      if (this->type == T_INTEGER)
        printf ("Integer: %d\n", ((t_integer *) this)->content);
      else if (this->type == T_STRING)
        printf ("String: \"%s\"\n", ((t_string *) this)->content);
    }
    
    int main (void)
    {
      t_integer i = { T_INTEGER, 42 };
      t_string s = { T_STRING, "Hello, world!" };
    
      t_base *object[] = { (t_base *) &i, (t_base *) &s, NULL };
    
      for (int i = 0; object[i]; i++)
        print_object (object[i]);
    
      return 0;
    }