{"id":31,"date":"2009-01-09T17:49:14","date_gmt":"2009-01-09T17:49:14","guid":{"rendered":"http:\/\/www.sanchitkarve.com\/blog\/?p=31"},"modified":"2009-01-09T17:49:14","modified_gmt":"2009-01-09T17:49:14","slug":"passing-2d-arrays-to-functions","status":"publish","type":"post","link":"https:\/\/www.sanchitkarve.com\/blog\/2009\/01\/09\/passing-2d-arrays-to-functions\/","title":{"rendered":"Passing 2D Arrays to functions"},"content":{"rendered":"<p>Passing 2D arrays without bounds is a common error people make.<br \/>\nSome people assume that if this works:<\/p>\n<pre lang=\"cpp\">void someFunc(int []);<\/pre>\n<p>then this should should work too:<\/p>\n<pre lang=\"cpp\">void anotherFunc(int [][]);<\/pre>\n<p>As normal as it may seem, it is incorrect.<\/p>\n<p>For passing 2D Arrays to a function, the column element must be mentioned like this:<\/p>\n<pre lang=\"cpp\">void func1(int [][upperSize]);<\/pre>\n<p>But why do we need to include the column element? If C\/C++ compilers can figure out the size of a 1D array then why not for 2D?<\/p>\n<p>That&#8217;s because 2D Array elements are stored consecutively as two 1D Arrays.<\/p>\n<p>Have a look at this code snippet:<\/p>\n<pre lang=\"cpp\">int main()\n{\nint x[][2]={1,2,3,4,5,6,7,8,9,10};\nint *p,*q;\nint i;\np=&amp;x[0][0];\nfor(i=0;i&lt;10;i++)\nprintf(\u201d%d \u201c,*(p+i));\n\nprintf(\u201dnn Now using pointer q:n\u201d);\n\nq=&amp;x[1][0];\nfor(i=0;i&lt;10;i++)\nprintf(\u201d%d \u201c,*(q+i));\n\nreturn 0;\n}<\/pre>\n<p>Run the Program. You\u2019ll get this as the output.<br \/>\n<code>1 2 3 4 5 6 7 8 9 10<\/p>\n<p>Now using pointer q:<br \/>\n3 4 5 6 7 8 9 10 0 4239532<\/code><br \/>\nNow change the number 2 in the array declaration to 5 like this:<\/p>\n<pre lang=\"cpp\">int x[][5]={1,2,3,4,5,6,7,8,9,10};<\/pre>\n<p>Run the code and observe the output:<br \/>\n<code>1 2 3 4 5 6 7 8 9 10<\/p>\n<p>Now using pointer q:<br \/>\n6 7 8 9 10 0 4239532 0 4235541 1<\/code><\/p>\n<p>As you can see, the output using the pointer <em>p<\/em> remains unchanged because we\u2019re setting it to the first element of the array itself, so it iterates to every element after it.<\/p>\n<p>But q is set to the first element of the 2nd row.<br \/>\nNow which one is the first element of the second row? It&#8217;s <strong>6<\/strong>. How do we know it?<br \/>\nIt\u2019s because we\u2019ve placed 5 in the column bracket to specify the bounds of each column.<\/p>\n<p>So if we don\u2019t place any value, the compiler gets confused as to how many numbers can be grouped into one column.<br \/>\nHence whenever such confusion occurs, the compiler generates the error: <em>size of the int[] is unknown or zero<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Passing 2D arrays without bounds is a common error people make. Some people assume that if this works: void someFunc(int []); then this should should work too: void anotherFunc(int [][]); As normal as it may seem, it is incorrect. For passing 2D Arrays to a function, the column element must be mentioned like this: void [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[9,21],"_links":{"self":[{"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/posts\/31"}],"collection":[{"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/comments?post=31"}],"version-history":[{"count":0,"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/posts\/31\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/media?parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/categories?post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sanchitkarve.com\/blog\/wp-json\/wp\/v2\/tags?post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}