title: Returning an array in C
body: <p>Hi, I'm a newbie in C (and in programming) and I'm trying to make a function that returns dynamically allocated array. It looks like:</p>
  
  <pre><code>int *returnArray(void) 
  { 
    int i, *p[4]; 
  
    for (i = 0; i &lt; 4; i++) 
      if ((p[i]=(int *)malloc(sizeof(int)*(4-i))) == NULL)
        return NULL;  
  
    return &amp;p[0][0];
  }
  </code></pre>
  
  <p>And calling to this function:</p>
  
  <pre><code>int *ar;
  
  if ((ar = returnArray()) == NULL) {
    /* ERROR */
    return 1;
  }
  </code></pre>
  
  <p>But it doesn't work at all. After calling the function I wish to use *ar like a normal 2D array + unallocate the memory at the end of programme. Even if I've looked at similar topics I don't know how to fix it.</p>
  
  <p>Thank you all for your help. I understand it much better now.</p>