title: Are "malloc(sizeof(struct a *))" and "malloc(sizeof(struct a))" the same?
body: <p>This question is a continuation of <a href="https://stackoverflow.com/questions/8761594/malloc-call-crashing-but-works-elsewhere">Malloc call crashing, but works elsewhere</a></p>
  
  <p>I tried the following program and I found it working (i.e. not crashing - and this was mentioned in the above mentioned link too). I May be lucky to have it working but I'm looking for a reasonable explanation from the SO experts on why this is working?!</p>
  
  <p>Here are some basic understanding on allocation of <code>memory</code> using <code>malloc()</code> w.r.t <code>structures</code> and <code>pointers</code></p>
  
  <ul>
  <li><code>malloc(sizeof(struct a) * n)</code> allocates <code>n</code> number of type <code>struct a</code> elements. And, this memory location can be stored and accessed using a <code>pointer-to-type-"struct a"</code>. Basically a <code>struct a *</code>.</li>
  <li><code>malloc(sizeof(struct a *) * n)</code> allocates <code>n</code> number of type <code>struct a *</code> elements. Each element can then point to elements of type <code>struct a</code>. Basically <code>malloc(sizeof(struct a *) * n)</code> allocates an <code>array(n-elements)-of-pointers-to-type-"struct a"</code>. And, the allocated memory location can be stored and accessed using a <code>pointer-to-(pointer-to-"struct a")</code>. Basically a <code>struct a **</code>.</li>
  </ul>
  
  <p>So when we create an <code>array(n-elements)-of-pointers-to-type-"struct a"</code>, is it </p>
  
  <ol>
  <li>valid to assign that to <code>struct a *</code> instead of <code>struct a **</code> ?</li>
  <li>valid to access/de-reference the allocated <code>array(n-elements)-of-pointers-to-type-"struct a"</code> using <code>pointer-to-"struct a"</code> ?</li>
  </ol>
  
  <p></p>
  
  <pre><code>data * array = NULL;
  
  if ((array = (data *)malloc(sizeof(data *) * n)) == NULL) {
      printf("unable to allocate memory \n");
      return -1; 
  }   
  </code></pre>
  
  <p>The code snippet is as follows:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(void) 
  {
      typedef struct { 
          int value1;
          int value2;
      }data;
  
      int n = 1000;
      int i;
      int val=0;
  
      data * array = NULL;
  
      if ((array = (data *)malloc(sizeof(data *) * n)) == NULL) {
          printf("unable to allocate memory \n");
          return -1; 
      }   
      printf("allocation successful\n");
  
      for (i=0 ; i&lt;n ; i++) {
          array[i].value1 = val++;
          array[i].value2 = val++;
      }   
  
      for (i=0 ; i&lt;n ; i++) {
          printf("%3d %3d %3d\n", i, array[i].value1, array[i].value2);
      } 
  
      free(array);
      printf("freeing successful\n");
  
      return 0;
  }
  </code></pre>
  
  <hr>
  
  <p>EDIT: 
  OK say if I do the following by <strong><em>mistake</em></strong></p>
  
  <pre><code>data * array = NULL;
  if ((array = (data *)malloc(sizeof(data *) * n)) == NULL) {
  </code></pre>
  
  <p>Is there a way to capture (during compile-time using any <code>GCC</code> flags) these kind of unintended programming typo's which could work at times and might blow out anytime! I compiled this using <code>-Wall</code> and found no warnings! </p>