Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Given this declaration:</p> <pre><code>int (*Arr)[8]; </code></pre> <p>you can assign it the address of an <code>int</code> array of size 8:</p> <pre><code>int data[8]; Arr = &amp;data; </code></pre> <p>You can modify <code>data</code> through <code>Arr</code>:</p> <pre><code>(*Arr)[i] = value; </code></pre> <p>Note the parentheses around <code>*Arr</code>. They're needed to dereference <code>Arr</code> <em>before</em> applying the index to it, since the index <code>[]</code> operator has a higher precedence.</p> <p>An important thing to understand here is that <code>data</code> is not dynamically allocated. If you keep its address around after it has gone out of scope, you'll get a dangling pointer (a pointer that points to something that no longer exists.)</p> <p>To dynamically allocate an array and assign it to <code>Arr</code>, you can use a typecast on the result of <code>new</code> as follows:</p> <pre><code>int (*Arr)[8] = reinterpret_cast&lt;int(*)[8]&gt;(new int[8]); </code></pre> <p>(A working example can be found on <a href="http://ideone.com/VWYxsE" rel="nofollow">ideone</a>.)</p> <p>Of course it doesn't make much sense to do this in such a roundabout way, as you can instead just:</p> <pre><code>int* Arr = new int[8]; </code></pre> <p>And of course you can use an <a href="http://www.cplusplus.com/reference/vector/vector" rel="nofollow">std::vector</a> instead and avoid manual memory management completely:</p> <pre><code>std::vector&lt;int&gt; Arr; </code></pre> <p>Which provides you with an array that grows on its own as needed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload