title: How to avoid unnecessary cast and extraneous variable involving ptr to ptr to ptr
body: <p>I have the following code that totally works 100% fine, no errors, compile or runtime.  But it's damn ugly because I have to cast and use an extraneous variable when I'm sure there's a way to do without either.</p>
  
  <pre><code>structMSGB ***init_bstack(int Blk_Size,int Blks_N)
    {
    structMSGB **Mp=calloc(Blk_Size,Blks_N);
    void *M=(void*)Mp+sizeof(structMSGB*)+sizeof(structMSGB*)*Blks_N;
  
    structMSGB ***startStack=(structMSGB***)Mp++;
  
    for(int i=0;i&lt;Blks_N;i++)
      {
      *Mp=M+(Blk_Size-sizeof(structMSGB*))*i-(i==1)*sizeof(structMSGB*);
      (*Mp)-&gt;blk_size=Blk_Size-sizeof(structMSGB)-sizeof(structMSGB*)-(i==0)*sizeof(structMSGB*);
      Mp++;
      }
  
    *startStack=(structMSGB **)Mp;
    return startStack;
    }
  </code></pre>
  
  <p>Specifically, it's the startStack variable that is pissing me off.  I feel there should be a way of doing without it altogether. The return value is the address  of a ptr to a ptr to a struct, i.e. It needs to return a ptr to a ptr to a ptr to a struct.</p>
  
  <p>The result returned is the starting address of a memory block that is Blk_Size bytes in size and is composed of the following in order:</p>
  
  <p>**ptr variable</p>
  
  <p>table of ptrs of Blk_N length</p>
  
  <p>sequential blocks of size Blk_Size - sizeof(ptr) except for the first block which is sizeof(ptr) smaller.</p>
  
  <p>It's done this way to ensure that the entire memory allocation is used, no more and no less.</p>