title: How can I allocate memory for array inside a function
body: <p>I am trying to receive a number from the user.
  And create an array with that number, but, inside a function.
  Here are my few attempts, I get into run time errors.
  Help is very much appreciated.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  int* Init(int* p, int num);
  int main() {
      int *p;
      int num, i;
      puts("Enter num of grades:");
      scanf("%d", &amp;num);
  
      Init(&amp;p, num);
      //for (i = 0; i &lt; num; i++)
      //{
      //  scanf("%d", &amp;p[i]);
      //}
      free(p);
  }
  int* Init(int* p, int num)
  {
      int *pp;
      p = (int *)malloc(num*sizeof(int));
      if (!pp)
      {
          printf("Cannot allocate memory\n");
          return;
      }
      p = pp;
      free(pp);
  }
  </code></pre>