title: Conditional struct type in C
body: <p>I just want to parse some data that can be present in one of two types. First one is</p>
  
  <pre><code>struct typeA {
    int id,
    char name[16],
    int value1,
    int value2,
    int value3,
    int value4
  } __attribute__ ((packed));
  </code></pre>
  
  <p>second possibility is that the data has a form with double name length</p>
  
  <pre><code>struct typeB {
    int id,
    char name[32],
    int value1,
    int value2,
    int value3,
    int value4
  } __attribute__ ((packed));
  </code></pre>
  
  <p>so far so good. Now I have two functions that parse these two</p>
  
  <pre><code>int parse_typeA(struct typeA *x){ /* do some stuff */ }
  int parse_typeB(struct typeB *x){ /* do some stuff */ }
  </code></pre>
  
  <p>Now this is obviously impractical if you have more types. How can I realize the parsing of both types using one single function and an additional parameter like</p>
  
  <pre><code>int parse_any_type(void *x, int type){ 
      /*
       *  WHAT TO DO HERE ??
       *  
       *  The following doesn't work
       *  
       *  if(type == 1)
       *    struct typeA *a = (struct typeA *)x;
       *  else
       *    struct typeB *a = (struct typeB *)x;
       */
      printf("%i\n", a-&gt;id);
      printf("%s\n", a-&gt;name);
      printf("%i\n", a-&gt;value1);
      printf("%i\n", a-&gt;value2);
      printf("%i\n", a-&gt;value3);
      printf("%i\n", a-&gt;value4);
  }
  </code></pre>
  
  <p>Anyone any idea?</p>