title: Initialize struct with malloc in C
body: <p>So I have what I think is a noob question. Sorry for that in advance, and for the grammar as english is not my primary language.</p>
  
  <p>So I have to make a game of checkers. I have some struct, defined by</p>
  
  <pre><code>struct game {
  int **board;
  int xsize, ysize;
  
  struct move *moves;
  int cur_player;
  };
  
  struct coord {
  int x, y;
  };
  
  struct move_seq {
  struct move_seq *next;
  struct coord c_old;
  struct coord c_new; 
  int piece_value;
  struct coord piece_taken;
  int old_orig;
  };
  
  struct move {
  struct move *next;
  struct move_seq *seq;
  };
  </code></pre>
  
  <p>And I have to initialize a struct game wih the fonction</p>
  
  <pre><code>struct game *new_game(int xsize, int ysize)
  </code></pre>
  
  <p>So, here's my problem. I call, for now, new_game always with 10 and 10 values for xsize an ysize. Then I initialize the board game, which I want to assign later.</p>
  
  <pre><code>int black = 1;
  int white = 5;
  int **board;
  int i;
  int j;
  int k;
  for(i=0;i&lt;xsize;i++)
  {
      for(j=0;j&lt;ysize;j++)
      {
          if(i&lt;(xsize/2) &amp;&amp; j&lt;(ysize/2) &amp;&amp; (i+j)%2!=0)
          {
              board[i][j] = black;
          }
          else if(i&gt;(xsize/2) &amp;&amp; j&gt;(ysize/2) &amp;&amp; (i+j)%2!=0)
          {
              board[i][j] = white;
          }
          else board[i][j] = 0;
      }
  }
  struct game *new = malloc (sizeof(struct game *));
  if (new == NULL) return NULL;
  </code></pre>
  
  <p>So, my problem is after that. I just have Segmentation Fault whatever I do with my struct new.</p>
  
  <p>I tried to do assign new->xsize = xsize and the same with ysize. I do a malloc for the board and the struct move, like I learned to do, but I kept getting this error of Segmentation Fault.</p>
  
  <p>So here's my real question: How to assign and initialize correctly a struct ? Do I Have to make a malloc for each of the member of struct game ? (I tried that too but without any success...)</p>
  
  <p>I don't necessarily want just the answer, I'd prefer to really understand what I have to do in this case and in general, to make less mistakes in the future.</p>
  
  <p>Thanks in advance for your help. </p>
  
  <p>Have a good day.</p>