title: C++: getting the row size of a multidimensional array passed to a function
body: <p>I'm trying to write a function that will print out the contents of a multidimensional array. I know the size of the columns, but not the size of the rows.</p>
  
  <p>EDIT: Since I didn't make this clear, the arrays passed to this function are NOT dynamically allocated. The sizes are known at compile time.</p>
  
  <p>I am testing it using a 3x2 array. Here is the function as it stands:</p>
  
  <pre><code>void printArrays(int array1[][2], int array2[][2]) {
      for (int i = 0; i &lt; 3; i++) {
          for (int j = 0; j &lt; 2; j++) {
              cout &lt;&lt; "\narray1[" &lt;&lt; i &lt;&lt; "][" &lt;&lt; j &lt;&lt; "] = "
                   &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; array1[i][j]
                   &lt;&lt; "\tarray2[" &lt;&lt; i &lt;&lt; "][" &lt;&lt; j &lt;&lt; "] = "
                   &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; array2[i][j];
          }
      }
  }
  </code></pre>
  
  <p>Obviously, this only works if I know the size of "i" is 3 (it is in this case). Ideally, however, I would like the function to work no matter what the size of the first dimension.</p>
  
  <p>I thought I would be able to do this using the sizeof() function, e.g.</p>
  
  <pre><code>int size = sizeof(array1);
  </code></pre>
  
  <p>... and do some math from there.</p>
  
  <p>Here's the odd part. If I use the sizeof() function inside the array, it returns a value of 4. I can use pointer notation to dereference the array:</p>
  
  <pre><code>int size = sizeof(*array1);
  </code></pre>
  
  <p>... but this actually returns a value of 8. This is odd, because the total size should be rows(which = 3) * columns(= 2) * sizeof(int)(= 4), or 24. And, indeed, this is the result, when I use sizeof(*array1) <em>outside</em> of the function.</p>
  
  <p>Does anyone know what is going on here? More importantly, does anyone have a solution?</p>