title: Segmentation fault when trying to print data from a file
body: <p>EDIT: I have fully fixed my code now. Here it is:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  
  int main(){
      int n, i , j;
      char **mensagem;
      char leitura[1000];
      FILE *arquivo;
  
      arquivo = fopen("entrada.txt", "r");
  
      fgets(leitura, 1000, arquivo);
  
      fclose(arquivo);
  
      arquivo = fopen("entrada.txt", "r");
  
      n = sqrt(strlen(leitura));
  
      mensagem = malloc(n * sizeof(char *));
  
      for (i = 0; i &lt; n; i++){
          mensagem[i] = malloc(n * sizeof(char));
      }
  
      for (j = 0; j &lt; n; j++){
          for (i = 0; i &lt; n; i++){
              fscanf(arquivo, "%c", &amp;mensagem[i][j]);
          }
      }
  
      for (j = 0; j &lt; n; j++){
          for (i = 0; i &lt; n; i++){
              if(mensagem[j][i] == '*'){
                  printf(" ");
                  i = n - 1;
              }
              else {
                  printf("%c", mensagem[j][i]);
              }
          }
      }
  
      fclose(arquivo);
      free(mensagem);
  
      return 0;
  }
  </code></pre>
  
  <p>The idea is to decypher messages like Cesar did back in the days :D. I read a coded message from a <code>.txt</code> file. I read the entire line then I dynamically allocate an array of arrays.
  That way I can print the columns and the message will be printed normally.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  int main(){
      int n, i , j;
      char **mensagem;
      char leitura[1000];
      FILE *arquivo;
      arquivo = fopen("entrada.txt", "r");
  
      fgets(leitura, 1000, arquivo);
  
      n = sqrt(strlen(leitura));
  
      mensagem = (char **)malloc(n * n * sizeof(char));
  
      for (j = 0; j &lt; n; j++){
          for (i = 0; i &lt; n; i++){
              fscanf(arquivo, "%c", &amp;mensagem[i][j]);
          }
      }
  
      for (j = 0; j &lt; n; j++){
          for (i = 0; i &lt; n; i++){
              if(mensagem[i][j] == '*'){
                  printf(" ");
                  i = n - 1;
              }
              else {
                  printf("%c", mensagem[i][j]);
              }
          }
      }
  
      fclose(arquivo);
      free(mensagem);
  
      return 0;
  }
  </code></pre>
  
  <p>Here's the data in the file:</p>
  
  <blockquote>
  <pre><code>AEEUMOLSHMSCGT*AGU2A***L:****T*****A
  </code></pre>
  </blockquote>
  
  <p>And here's the expected output:</p>
  
  <blockquote>
  <pre><code>ALG2: ESTA EH UMA MSG OCULTA
  </code></pre>
  </blockquote>
  
  <p>And here's the logic behind it:</p>
  
  <blockquote>
  <pre><code>A E E U M O
  L S H M S C
  G T * A G U
  2 A * * * L
  : * * * * T
  * * * * * A
  </code></pre>
  </blockquote>
  
  <p>I do know that the problem is exactly with the call of <code>printf</code>, but I'm unsure on how to fix it.</p>