title: How to fix this code written in C?
body: <p>It works perfectly well for a 20-20 table but for anything else it crashes.
  the file is in this link:<a href="http://www.csd.uoc.gr/~hy100/glid.txt" rel="nofollow">http://www.csd.uoc.gr/~hy100/glid.txt</a>.
  I can not figure out what the problem is because when I put an 20-20 table it works and when 
  I give a 21-20 table it does the job perfectly fine but crashes before being able to return 0; .</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int height, length;
  char **world, **new_world;
  
  int load_world_from_file(char filename[]) {
  
    int i, j;
    char ch;
  
    FILE *fp;
    fp = fopen(filename, "r");
  
    if (fp == NULL ) {
      perror("Error while opening the file.\n");
      exit(EXIT_FAILURE);
    }
  
    fscanf(fp, "%d %d", &amp;height, &amp;length);
    printf("here:%d,%d\n", height, length);
    /*dilosi dunamika tou pinaka kataxoriseis*/
    /* declaration under the table entries */
    world = (char **) malloc(sizeof(char *) * (height + 1));/*height + 1 ('\0')*/
    for (i = 0; i &lt; 2 * height; i++) {
      /* height + height ('|') + 1    ('\0') */
      world[i] = (char *) malloc(sizeof(char) * (length + length + 1));
    }
  
    /* height + 1 ('\0') */
    new_world = (char **) malloc(sizeof(char *) * (height + 1));
    for (i = 0; i &lt; 2 * height; i++) {
      /* height + height ('|') +  1 ('\0') */
      new_world[i] = (char *) malloc(sizeof(char) * (length + length + 1));
    }
  
    printf("The contents of %s file are :\n", filename);
  
    i = 0;
    j = 0;
    while ((ch = fgetc(fp)) != EOF) {
      i++;
      if (i = length + length + 1) {
        j++;
        i = 0;
      }
      world[j] = ch;
      printf("%c", world[j]);
    }
    printf("\n");
    for (i = 0; i &lt; (((2 * length) + 2) * height) + 1; i++) {
      printf("%c", world[i]);
    }
  
    fclose(fp);
    return 0;
  }
  
  int main() {
  
    char filename[30], filename2[30];
  
    printf("Enter the name of file you want to open:\n");
    scanf("%s", &amp;filename);
  
    load_world_from_file(filename);
  
    return 0;
  }
  </code></pre>
  
  <p>Your help is much appreciated.
  IT</p>