title: How to reference to struct inside struct
body: <p>I have 2 structs that are closely related to each other so i want one struct to reference to the other. Like so:</p>
  
  <pre><code>//inside maze.h
  typedef struct{
      char * maze;
      int height, length, cols, rows;
  } maze_t;
  
  //inside walker.h
  typedef struct {
      int row, col, end_row, end_col, dir, origin;
      maze_t * maze;
  } walker_t;
  </code></pre>
  
  <p>But here is my problem: When i want to print the string walker->maze->maze i get a segmentation fault. It's a lot of code, but i dont know where i made the mistake. The segmentation fault occurs in the <code>move_walker</code> function.</p>
  
  <p>My code:</p>
  
  <p>maze.c:</p>
  
  <pre><code>#include "maze.h"
  #include "string.h"
  #include "stdio.h"
  #include "stdlib.h"
  
  /* Incomplete definitions of the maze support function . */
  void init_maze(maze_t* maze, FILE * pFile) {
  
      int result;
  
      // obtain file size:
      fseek(pFile , 0 , SEEK_END);
      int lSize, stringPtr;
      lSize= ftell(pFile);
      rewind (pFile);
  
      // allocate memory to contain the whole file:
      char* string = malloc (sizeof(lSize);
      if (string == NULL) {fputs ("Memory error",stderr); exit (2);}
  
      // copy the file into the buffer:
      result = fread (string,1,lSize,pFile);
      if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
  
      fclose(pFile);
  
      maze-&gt;maze = malloc (strlen(string) + 1);
  
      stringPtr = find_letter_in_string('\n', string);
      strcpy(maze-&gt;maze, &amp;string[stringPtr+1]);
  
      maze-&gt;rows = atoi(string);
  
      stringPtr = find_letter_in_string(',', string);
      maze-&gt;cols = atoi(&amp;string[stringPtr+1]);
  
      printf("Maze has %d rows and %d columns \n", maze-&gt;rows, maze-&gt;cols);
  
      return;
  }
  </code></pre>
  
  <p>walker.h:</p>
  
  <pre><code>#include "maze.h"
  #include "walker.h"
  #include "stdlib.h"
  #include "stdio.h"
  
  
  walker_t* init_walker(maze_t * maze) {
  
      walker_t* walker = malloc(sizeof(walker_t));
  
      walker-&gt;dir = 0;
  
      printf("Made room for walker.\n");
  
      walker-&gt;maze = maze;
      locate(maze, 'S',&amp;walker-&gt;row, &amp;walker-&gt;col);
  
      printf("Start coordinates: %d, %d.\n", walker-&gt;row, walker-&gt;col);
  
      locate(maze, 'E',&amp;walker-&gt;end_row, &amp;walker-&gt;end_col);
  
      return walker;
  }
  
  int move_walker(walker_t * walker, int row, int col) {
  
      printf("maze: %s", walker-&gt;maze-&gt;maze);
  
      printf("check: %d\n", check_move(walker-&gt;maze, row, col));
      if(! check_move(walker-&gt;maze, row, col)){
      printf("hello world");
          return 0;
      }
      walker-&gt;row = row;
      walker-&gt;col = col;
      return 1;
  }
  </code></pre>
  
  <p>main.c:</p>
  
  <pre><code>maze = malloc( sizeof (maze_t));
  
  FILE * pFile = fopen(argv[1],"r");
  if(pFile == NULL){
      printf("No such file!\n");
      return 0;
  }
  
  init_maze(maze, pFile);
  printf("Scrambled the maze.\n");
  
  
  walker = init_walker(maze);
  printf("Woken the walker.\n");
  </code></pre>
  
  <p>Sorry for spelling mistakes and such, i have dyslexia next to the fact that this is not my native language.</p>