{"id":4778,"date":"2014-03-30T15:20:23","date_gmt":"2014-03-30T15:20:23","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/heap-allocate-a-2d-array-not-array-of-pointers-collection-of-common-programming-errors\/"},"modified":"2014-03-30T15:20:23","modified_gmt":"2014-03-30T15:20:23","slug":"heap-allocate-a-2d-array-not-array-of-pointers-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/heap-allocate-a-2d-array-not-array-of-pointers-collection-of-common-programming-errors\/","title":{"rendered":"Heap allocate a 2D array (not array of pointers)-Collection of common programming errors"},"content":{"rendered":"<p>This is easy assuming you don&#8217;t need compatibility with the ancient C89 standard (among current C compilers, only MSVC and a few embedded-target compilers are that backwards). Here&#8217;s how you do it:<\/p>\n<pre><code>int (*array)[cols] = malloc(rows * sizeof *array);\n<\/code><\/pre>\n<p>Then <code>array[a][b]<\/code> is valid for any <code>a<\/code> in <code>[0,rows)<\/code> and <code>b<\/code> in <code>[0,cols)<\/code>.<\/p>\n<p>In the language of the C standard, <code>array<\/code> has <em>variably-modified type<\/em>. If you want to pass the pointer to other functions, you&#8217;ll need to repeat this type in the function argument list and make sure that at least the number of columns is passed to the function (since it&#8217;s needed as part of the variably-modified type).<\/p>\n<p><strong>Edit:<\/strong> I missed the fact that OP only cares about a fixed size, 512&#215;256. In that case, C89 will suffice, and all you need is:<\/p>\n<pre><code>int (*array)[256] = malloc(512 * sizeof *array);\n<\/code><\/pre>\n<p>The exact same type can be used in function argument lists if you need to pass the pointer around between functions (and also as a function return type, but for this use you might want to typedef it&#8230; \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is easy assuming you don&#8217;t need compatibility with the ancient C89 standard (among current C compilers, only MSVC and a few embedded-target compilers are that backwards). Here&#8217;s how you do it: int (*array)[cols] = malloc(rows * sizeof *array); Then array[a][b] is valid for any a in [0,rows) and b in [0,cols). In the language [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4778","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4778","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4778"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4778\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4778"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}