title: How to access members through a void pointer
body: <p>Started by trying to write a small program to translate basic arithmetic into English, I end up building a binary tree(which is inevitably very unbalanced) to represent the order of evaluations.
  First, I wrote</p>
  
  <pre><code>struct expr;
  
      typedef struct{
      unsigned char entity_flag;  /*positive when the oprd
      struct represents an entity 
       ---a single digit or a parenthesized block*/                      
      char oprt;
  
      expr * l_oprd;// these two point to the children nodes 
      expr * r_oprd;
      } expr;
  </code></pre>
  
  <p>However, to efficiently represent single digits, I prefer </p>
  
  <pre><code>typedef struct{
   unsigned char entity_flag;
   int ival;
  } digit;
  </code></pre>
  
  <p>Since now the "oprd" feild of each "expr" struct may be either of the above struct-s, I now shall modify their types to</p>
  
  <pre><code>void * l_oprd;
  void * r_oprd;
  </code></pre>
  
  <p>Then there comes the "central question":
   how can you access members through a void pointer?
  please see the follow code</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  
  
  typedef struct {
  int i1;
  int i2;} s;
  main(){
  void* p=malloc(sizeof(s));
  
  //p-&gt;i1=1;
  //p-&gt;i2=2;
  
  *(int*)p=1;
  *((int *)p+1)=2;
  printf("s{i1:%d, i2: %d}\n",*(int*)p,*((int *)p+1));
  }
  </code></pre>
  
  <p>The compiler wouldn't accept the commented version!
  Do I have to do it with the cluttered approach above?</p>
  
  <p>please help.</p>
  
  <p>PS: as you have noticed ,each struct-s above possess a field of the name "entity_flag", thus </p>
  
  <pre><code>void * vp;
  ...(giving some value to vp)
  unsigned char flag=vp-&gt;entity_flag;
  </code></pre>
  
  <p>may extract the flag regardless of what void points to, is this allowed in C? or even "safe" in C?</p>