title: Returning dynamic array through void function in C
body: <p>In my C program, I use a void function with the following arguments:
  One 2D int array, one int pointer that will be used to create the new dynamic array and a last int pointer which will hold a number of counts that will occur inside the function.
  So the dynamic array is created in the function using malloc and everything works okay, until I print its elements in main() after calling the function. What I get is rubbish instead of the numbers I should see. Here's the function code:</p>
  
  <pre><code>void availableMoves(int array[][3], int *av, int *counter)
  {
      int i, j;
      for (i=0; i&lt;3; i++)
      {
          for (j=0; j&lt;3; j++)
          {
              if (array[i][j] == E)
              {
                  printf("%d ", 3*i + j + 1);
                  (*counter)++;
              }
          }
      }
      av = (int *) malloc(*counter * sizeof(int));
      if (av == NULL)
      {
          printf("ERROR!");
      }
      else
      {
          for (i=0; i&lt;*counter; i++)
              *(av + i) = 0;
          int pos = 0;
          for (i=0; i&lt;3; i++)
          {
              for (j=0; j&lt;3; j++)
              {
                  if (array[i][j] == E)
                  {
                      *(av + pos++) = 3*i + j + 1;
                  }
              }
          }
      }
  }
  </code></pre>