title: Problems with allocating memory for a matrix in c
body: <p>I'm trying to allocate memory to a matrix in a function and then print its values.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  void cria_ilhas(int** ilhas, int n){
      int i, p;
      ilhas = (int**) malloc(n*sizeof(int*));
      for (i=0;i&lt;n;i++){
          ilhas[i] = (int*) malloc(n*sizeof(int));
          for (p=0; p&lt;n; p++)
              ilhas[i][p] = 4;
      }
      printf("value %d\n",ilhas[0][0]);
  }
  
  void main(){
      int n=5, i, j;
      int **ilhas;
      cria_ilhas(ilhas, n);
      for(i=0; i&lt;n; i++){
          for(j=0;j&lt;n;j++){
              printf("%d ",ilhas[i][j]);
          }
          printf("\n");
      }   
  }
  </code></pre>
  
  <p>But this is the output:</p>
  
  <pre><code>value 4
  Segmentation fault
  </code></pre>
  
  <p>Why i'm having segmentation fault?</p>
  
  <p>How can I use memset in this kind of matrix?</p>