Skip to content
Snippets Groups Projects
Select Git revision
  • a095fa9e7977af44b1ea6da43e48ffa759eca088
  • 2025ss default
  • 2024ss
  • 2023ss
  • 2022ss
  • 2021ss
  • 2020ss
  • 2019ss
  • 2018ss
9 results

arrays-and-pointers-05.c

Blame
  • objects-5.c 566 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;
    }