title: Array Indexing Error in Multidimensional Column Shifting (pointers being used)
body: <p>I have gotten some insight on how to deal with multidimensional array columns.</p>
  
  <p>Below is some compilable code that runs and performs okay for the most part.</p>
  
  <p>I want to use the very logic I am using right now, and it seems sound to me (as a novice at least), however I am getting very nasty output.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdbool.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  </code></pre>
  
  <blockquote>
    <p>Here I initialize an empty array which you will see later in main:</p>
  </blockquote>
  
  <pre><code>void initializeGrid(int row, int col, int grid[][col])
  {
      int count = 1;
      for (int i = 0; i &lt; row; i++)
      {
          for (int j = 0; j &lt; col; j++)
          {
              grid[i][j] = count;
              count++;
          }
      }
  }
  </code></pre>
  
  <blockquote>
    <p>Here I print the initial case of the grid that will later be
    manipulated. This should be reused:</p>
  </blockquote>
  
  <pre><code>void printGrid(int row, int col, int grid[][col])
  {
          for (int i = 0; i &lt; row; i++)
          {
              for (int j = 0; j &lt; col; j++)
              {
                  printf("%3d", grid[i][j]);
              }
              printf("\n");
          }
  }
  </code></pre>
  
  <blockquote>
    <p>Here I get the location of a specific value by its row and its column
    and I store it to the pointers r, and c:</p>
  </blockquote>
  
  <pre><code>bool findUnit(int unit, int row, int col, int grid[][col], int *r, int *c)
  {
      for (int i = 0; i &lt; row; i++)
      {
          for (int j = 0; j &lt; col; j++)
          {
              if (grid[i][j] == unit)
              {
                  *r = i;
                  *c = j;
                  return true;
              }
          }
      }
      return false;
  }
  </code></pre>
  
  <blockquote>
    <p>Here I shift up using very elementary array mechanics and I felt so
    confident about it until seeing my output:</p>
  </blockquote>
  
  <pre><code>bool shiftUp(int unit, int row, int col, int grid[][col])
  {
      int r = 0;
      int c = 0;
      if (findUnit(unit, row, col, grid, &amp;r, &amp;c))
      {
          int i;
          int temp = grid[0][c];
          for (i = 1; i &lt;= row; i++)
          {
              grid[i - 1][c] = grid[i][c];
          }
          grid[r][c] = temp;
          return true;
      }
      return false;
  }
  </code></pre>
  
  <blockquote>
    <p>Here I run the shift up command or I quit. Assume that run can make
    array move in other directions as well. It does not work for any
    direction yet, but I assume if I can fix one, I can fix all:</p>
  </blockquote>
  
  <pre><code>bool run(char cmd[10], int row, int col, int grid[][col])
  {
      int unit = 0;
      printf("$: ");
      scanf(" %s %d", cmd, &amp;unit);
  
      if (strcmp(cmd, "up") == 0)
      {
          shiftUp(unit, row, col, grid);
          return true;
      }
      if (strcmp(cmd, "quit") == 0)
      {
          return false;
      }
      return false;
  }
  </code></pre>
  
  <blockquote>
    <p>Here I execute the code:</p>
  </blockquote>
  
  <pre><code>int main()
  {
      int row = 4;
      int col = 4;
      int grid[row][col];
      char cmd[32];
  
      initializeGrid(row, col, grid);
      printGrid(row, col, grid);
  
      while (run(cmd, row, col, grid))
      {
          printGrid(row, col, grid);
      }
      return 0;
  }
  </code></pre>
  
  <p>This is all one file. Here also is the disturbing output I am getting and the expected output.</p>
  
  <pre><code>  1  2  3  4
    5  6  7  8
    9 10 11 12
   13 14 15 16
  $: up 9
    5  2  3  4
    9  6  7  8
    1 10 11 12
  1347447432 14 15 16
  $: quit
  
  ^C
  </code></pre>
  
  <p>Notice even when I type quit it does not quit. </p>
  
  <p>Expected output should be something like (in a smaller grid i.e. 3x3):</p>
  
  <pre><code>1 2 3             1 5 3
  4 5 6 --(up 5)--&gt; 4 8 6
  7 8 9             7 2 9
  </code></pre>