title: Visual Studio dynamic allocation of a multidimensional array
body: <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt; 
  
  int main(void){
  int n, i, j;
  
  printf("Enter the number of rows and columns: ");
  scanf("%d", &amp;n);
  
  int **matrix = malloc(n * sizeof(int *));
  
  for(i = 0; i &lt; n; i++)
      matrix[i] = malloc(n * sizeof(int *));
  
  // Read the matrix
  for(i=0; i&lt;n; i++)
     for(j=0; j&lt;n; j++){
              printf("matrix[%d][%d]= ",i+1,j+1);
              scanf("%d",&amp;matrix[i][j]);
              }
  
  // Print the matrix
  for(i = 0; i &lt; n; i++){
      printf("%\n");
      for(j = 0; j &lt; n; j++)
          printf("%d", matrix[i][j]);
  }
  
  
  // Free the allocated memory
  for(i = 0; i &lt; n; i++)
    for(j = 0; j &lt; n; j++)
       free((void *)matrix[i]);
  
  free(matrix);
  
  // Just checking if the memory has been freed
  for(i = 0; i &lt; n; i++){
    printf("%\n");
       for(j = 0; j &lt; n; j++)
          printf("%d ", matrix[i][j]);
  }
  
  system("PAUSE");
  return 0;
  }
  </code></pre>
  
  <p>I was just trying to dynamically allocate a matrix. Even though in CodeBlocks everything works fine, since we work with Visual Studio at the University, I decided to test the code in VS 2010. I was surprised that I have so many errors and that the code wont compile.
  I would like to know how could I fix the problem so VS would compile the code just fine.</p>
  
  <p>Here are the errorrs:</p>
  
  <pre><code>(10): error C2143: syntax error : missing ';' before 'type'
  (13): error C2065: 'matrix' : undeclared identifier
  (13): error C2109: subscript requires array or pointer type
  (19): error C2065: 'matrix' : undeclared identifier
  (19): error C2109: subscript requires array or pointer type
  (26): error C2065: 'matrix' : undeclared identifier
  (26): error C2109: subscript requires array or pointer type
  (33): error C2065: 'matrix' : undeclared identifier
  (33): error C2109: subscript requires array or pointer type
  (33): error C2198: 'free' : too few arguments for call
  (35): error C2065: 'matrix' : undeclared identifier
  (35): warning C4022: 'free' : pointer mismatch for actual parameter 1
  (41): error C2065: 'matrix' : undeclared identifier
  (41): error C2109: subscript requires array or pointer type
  </code></pre>