title: Using Scanf for Storing Input in 2d Arrays
body: <p>I want to scan input and save it in a square 2d array.</p>
  <p>The first two digits are saved in seperate variables, the first digit is a target number (irrelevant here), the second digit gets saved in variable <strong>m</strong>, i.e. <strong>m</strong> = 5 in this case. <strong>m</strong> is the number of rows/colums of the square matrix. The rest of the input should be saved in the array.
  For this particular input, I get a segmentation-fault and random numbers are printed on the screen.
  I used some printf statements to trace where things go wrong, and I noticed that the index <strong>i</strong> in the first loop jumped from 2 to 11 in one scenario, for other input it jumped to 33.
  Thanks for your help! I hope I am not missing an obvious mistake.</p>
  <p>Input: (each row is seperated by the previous by pressing enter.)</p>
  <blockquote>
  <p>42 5</p>
  <p>0 3 7 9 10</p>
  <p>9 13 20 5 20</p>
  <p>12 11 33 0 12</p>
  <p>17 39 22 3 18</p>
  </blockquote>
  <p>My code:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main (int argc, char* arv[]){
  
    int target;               // for later processing, irrelevant here
    int m;                    // m = #rows and #columns of array
    int array[m][m];
    scanf(&quot;%d %d&quot;, &amp;target, &amp;m);
   
    int i, k;
    for(i = 0; i &lt; m; i++){
      for(k = 0; k &lt; m; k++){
         scanf(&quot;%d&quot;, &amp;(array[i][k]));    // save value in array.
      }
    }
                                        // the problem occurs before this point.
    for(i = 0; i &lt; m; i++){
       for(k = 0; k &lt; m; k++){
         printf(&quot;%2d&quot;, array[i][k]);    // print array.
       }
       printf(&quot;\n&quot;);
    }  
  
    return 0;
  }
  </code></pre>