Skip to content
Snippets Groups Projects
Select Git revision
  • 7e093adc391080059f5439cb5dcde46aa305bdd0
  • master default protected
  • cvh
  • main
4 results

setup.mathreader.sh

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;
    }